public class ShellSort {
public static void main(String[] args){
int[] arr={3,1,15,29,16,9,66,84,42,36,99};
System.out.print("给定一个无序的数组:");
for(int i:arr){
System.out.print(i+" ");
}
System.out.println("\n");
ShellSort shell=new ShellSort();
shell.shellSort(arr);
System.out.print("希尔排序后的数组:");
for(int i:arr){
System.out.print(i+" ");
}
}
private void shellSort(int[] arr){
for(int gap=arr.length/2;gap>=1;gap/=2){
for(int i=gap;i<arr.length;i++){
int j=i-gap;
int temp=arr[i];
while(j>=0&&arr[j]>arr[j+gap]){
arr[j+gap]=arr[j];
j-=gap;
}
arr[j+gap]=temp;
}
}
}
}
给定一个无序的数组:3 1 15 29 16 9 66 84 42 36 99
希尔排序后的数组:1 3 9 15 16 29 36 42 66 84 99
|