public class SortDemo2 {
public static void main(String[] args) {
int[] a = creatInt2();
shellSort(a);
}
// 正序
private static int[] creatInt() {
int[] a = new int[10000001];
for (int i = 0; i < 10000001; i++) {
a[i] = i;
}
return a;
// 100001 开始时间:1525767016491,结束时间:1525767016530,用了多长时间:39
//10000001开始时间:1525768053766,结束时间:1525768055427,用了多长时间:1661
}
// 反序
private static int[] creatInt2() {
int[] a = new int[10000001];
for (int i = 10000000, j = 0; j < 10000001; i--, j++) {
a[j] = i;
}
return a;
// 100001 开始时间:1525767003666,结束时间:1525767003708,用了多长时间:42
//10000001 开始时间:1525768094010,结束时间:1525768096270,用了多长时间:2260
}
// 希尔排序
public static void shellSort(int[] a) {
Date d = new Date();
long start = d.getTime();
int j;
for (int gap = a.length / 2; gap > 0; gap /= 2) {//要排序的hk a[i]<=a[i+hk]
for (int i = gap; i < a.length; i++) {
Integer temp = a[i];
for (j = i; j >= gap && temp.compareTo(a[j - gap]) < 0; j -= gap)//如果a[i]<a[i-hk] ===a[i]>a[i+hk]
a[j] = a[j - gap];
a[j] = temp;
}
}
/*for (int i : a) {
System.out.print(i + ",");
}
System.out.println();*/
Date d2 = new Date();
long end = d2.getTime();
System.out.println("开始时间:" + start + ",结束时间:" + end + ",用了多长时间:" + (end - start));
}
}
Hill sort study notes
Guess you like
Origin http://10.200.1.11:23101/article/api/json?id=326804409&siteId=291194637
Recommended
Ranking