☆优化:去除排序完成后的,轮数空转时间(bubblingSortx方法)
冒泡排序及优化代码:冒泡排序和优化
测试代码:
package com.ts.w;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Array1millionSize {
public static int [] getList(){
int rdNum = (int) (Math.random() * 100);
// List<Integer> list = new ArrayList<Integer>(1000000);
// for(int i = 0; i < 1000000; i++){
// list.add(i+rdNum);
// }
int [] intArray = new int[1000000];
for(int i = 0; i < 1000000; i++){
intArray[i] = i + rdNum;
}
return intArray;
}
public static void main(String[] args) {
long sTime = System.currentTimeMillis();
int [] list = getList();
long eTime = System.currentTimeMillis();
System.out.println("生成1百万int数组的time:"+(eTime - sTime)+"ms");
// int listLen = list.length;
// long sTime0 = System.currentTimeMillis();
// for(int i = 0;i < listLen; i++){
// System.out.println(list[i]);
// }
// long eTime0 = System.currentTimeMillis();
// System.out.println("1百万int数组打印时间time0:"+(eTime0 - sTime0)+"ms");
int [] temp1 = list;
int [] temp2 = list;
long sTime1 = System.currentTimeMillis();
int [] temp3 = BubblingSort.bubblingSort(temp1);
long eTime1 = System.currentTimeMillis();
System.out.println("bubblingSort方法排序时间1time:"+(eTime1 - sTime1)+"ms");
long sTime2 = System.currentTimeMillis();
int [] temp4 = BubblingSort.bubblingSortx(temp2);
long eTime2 = System.currentTimeMillis();
System.out.println("bubblingSortx方法排序时间2time:"+(eTime2 - sTime2)+"ms");
if(Arrays.equals(temp3, temp4)){
System.out.println("内容相同数组");
}else{
System.out.println("不是内容相同数组");
}
}
}
测试结果:
生成1百万int数组的time:5ms
bubblingSort排序中……
bubblingSort方法排序时间1time:143847ms
bubblingSortx排序中……
bubblingSortx方法排序时间2time:3ms
内容相同数组
注:在两个方法开始时,加入"排序……",等待显示!
可见空转确实有很长时间!(暂时,不确定,计算机对重复操作的处理!)