/*
希尔排序
*/
public class ShellSort {
public static void main(String[] args) {
for (int as = 0;as<10;as++) {
int[] a = new int[10000];
Random random = new Random();
for (int i = 0; i < 10000; i++) {
int rdm = random.nextInt(10000);
a[i] = rdm;
}
long start = currentTimeMillis();
sort(a);
long end = currentTimeMillis();
System.out.println("使用的时间 "+(end -start) +"毫秒" );
// print(sort);
// System.out.println(check(a, sort));
}
}
static void sort ( int[] arr){
int[] arrSort = new int[arr.length];
System.arraycopy(arr, 0, arrSort, 0, arr.length);
for (int gap = arrSort.length / 2; gap > 0; gap /= 2) {
for (int i = gap; i < arrSort.length; i++) {
for (int j = gap; j < arrSort.length; j += gap) {
if (arrSort[j] < arrSort[j - 1]) swap(arrSort, j, j - 1);
}
}
}
System.out.println( check(arr, arrSort));
}
static void swap ( int[] arr, int i, int j){
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
static int[] print ( int[] arr){
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
return arr;
}
//对数器
static String check ( int[] arr1, int[] arr2){
Arrays.sort(arr1);
for (int i = 0; i < arr1.length; i++) {
if (arr1[i] != arr2[i]) return "第" + i + "个元素出错";
}
return "正确";
}
}
|