import java.util.Arrays;
import java.util.Random;
?
public class 冒泡排序 {
public static void main(String[] args) {
int[] array = new int[10];
Random r = new Random(); //生成随机数种子
for (int i = 0; i < array.length; i++) {
array[i] = r.nextInt(1000); //设置随机数范围[0,1000)
}
System.out.println(Arrays.toString(array)); //调用Arrays的toString方法,传入array数组
sort(array);
System.out.println(Arrays.toString(array));
}
?
public static int[] sort(int[] array){ //冒泡排序
int temp = 0;
for (int i = 0; i < array.length-1; i++) {
for (int j = 0; j < array.length-1-i; j++){
if (array[j+1]<array[j]){
temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
}
return array;
}
}
?
原文:https://www.cnblogs.com/16lee/p/14413967.html