import java.util.Random;
public class Test1 {
public static void main(String[] args) {
int[] list=new int[50];
int temp=0;
Random random=new Random();
for(int j=0;j<list.length;j++)
list[j]=random.nextInt(100);
for(int i=0;i<list.length-1;i++) {
for(int k=i+1;k<list.length;k++) {
if(list[i]>list[k]) {
temp=list[i];
list[i]=list[k];
list[k]=temp;
}
}
}
System.out.println("\n通过冒泡排序法的结果:\n");
for(int i=0;i<list.length;i++)
System.out.print(list[i]+" ");
}
}
运行结果如下(时间复杂度为O(n²)):
原数组元素:
10 90 76 86 36 41 83 76 35 85 49 77 57 40 36 24 95 62 17 50 55 87 24 64 90 63 33 96 13 38 71 99 44 91 6 92 68 13 40 88 83 45 32 2 90 12 63 89 90 75
通过冒泡排序法的结果:
2 6 10 12 13 13 17 24 24 32 33 35 36 36 38 40 40 41 44 45 49 50 55 57 62 63 63 64 68 71 75 76 76 77 83 83 85 86 87 88 89 90 90 90 90 91 92 95 96 99