public class Demo {
public static void main(String[] args) {
Random rd=new Random();
int[] arr=new int[320];
for (int i = 0; i < arr.length; i++) {
arr[i]=rd.nextInt(Integer.MAX_VALUE);
}
System.out.println("原数组为:"+arrToString(arr));
long start = System.currentTimeMillis();
arrSort(arr);
System.out.println("排序后为:"+arrToString(arr));
System.out.println("排序后为:"+Arrays.toString(arr));
long end = System.currentTimeMillis();
System.out.println("排序共耗时:"+(end-start)+"毫秒");
}
private static void arrSort(int[] arr) {
for(int i=0;i<arr.length-1;i++){
for(int j=0;j<arr.length-1-i;j++){
if(arr[j+1]<arr[j]){
int temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
}
private static String arrToString(int[] arr) {
StringBuilder sb=new StringBuilder();
sb.append("[");
for (int i = 0; i < arr.length; i++) {
sb.append(arr[i]+", ");
}
sb.append("]");
return sb.toString();
}
}
|
|