希尔排序
public class ShellSort {
public static void main(String[] args) {
int[] arr = { 8, 9, 1, 7, 2, 3, 5, 4, 6, 0};
int[] arr2 = new int[80000];
for (int i = 0; i < 80000; i++) {
arr2[i] = (int) (Math.random() * 80000);
}
long startTime = System.currentTimeMillis();
shellSort_Plus(arr2);
long endTime = System.currentTimeMillis();
System.out.println("程序运行时间: "+(endTime-startTime)+"ms");
}
public static void shellSort(int[] arr){
int temp = 0;
int count = 0;
for(int gap=arr.length/2;gap>0;gap /= 2){
for(int i=gap;i<arr.length;i++){
for (int j=i-gap;j>=0;j -= gap){
if (arr[j]>arr[j+gap]){
temp = arr[j];
arr[j] = arr[j+gap];
arr[j+gap] = temp;
}
}
}
}
}
public static void shellSort_Plus(int[] arr){
for (int gap=arr.length/2;gap>0;gap/=2){
for (int i=gap;i<arr.length;i++){
int j = i;
int temp = arr[j];
while(j-gap>=0 && temp<arr[j-gap]){
arr[j] = arr[j-gap];
j -= gap;
}
arr[j] = temp;
}
}
}
}