- 冒泡排序的时间复杂度:O(n2)
- Arrays.sort的时间复杂度:O(nlogn)
import java.util.Arrays;
public class Main {
public static int a[] = new int[10000* 10 ];
public static int n = 10000 * 10;
public static void main(String[] args) {
for(int i = 0; i < 10000 * 10; i ++) {
a[i] = i * i +i * 2 + 1 ;
}
//冒泡排序
long start = System.currentTimeMillis();
bubbleSolt();
long end = System.currentTimeMillis();
System.out.println(end - start + "ms");
//系统自定义
start = System.currentTimeMillis();
Arrays.sort(a);
end = System.currentTimeMillis();
System.out.println(end - start + "ms");
}
private static void bubbleSolt() {
int t;
for(int i = 0; i < n; i ++) {
for(int j = 0; j < n - i - 1; j ++) {
if(a[j] > a[j + 1]) {
t = a[j];
a[j] = a[j + 1] ;
a[j + 1] = t;
}
}
}
}
}
运行结果:
5044ms
2ms