[Java] 纯文本查看 复制代码 import java.util.ArrayList;
import java.util.List;
import java.util.Random;
//随机产生20个1-100的整数,写入数组,排序
public class Demo {
public static void main(String[] args) {
List<Integer> list1 = new ArrayList<Integer>();
Random r = new Random();
for(int i=0;i<20;i++){
list1.add(r.nextInt(100));
}
System.out.println("初始顺序是:");
System.out.println(list1);
int min = list1.get(0);
for(int i=0;i<list1.size();i++){
for(int j=i+1;j<list1.size();j++){
if(list1.get(j)<list1.get(i)){
min = list1.get(i);
list1.set(i,list1.get(j));
list1.set(j, min);
}
}
}
System.out.println("冒泡法排序后从小到大是:");
System.out.println(list1);
}
} 这是我下午写的
|