public class Method {//以下是希尔排序的两种实现方法
public void shellSort(int[] a) {//比较慢
/*//int[] a={5,9,4,2,1,10,7,8,3,6},假如十个数;
int temp=0;
for (int i = 5; i < a.length; i++) {
*//**//*
for (int j=i-5;j>=0;j=j-5){
if (a[j]>a[j+5]){
temp=a[j];
a[j]=a[j+5];
a[j+5]=temp;
}
}
}
System.out.println("one:"+Arrays.toString(a));
for (int i = 2; i < a.length; i++) {
for (int j=i-2;j>=0;j=j-2){
if (a[j]>a[j+2]){
temp=a[j];
a[j]=a[j+2];
a[j+2]=temp;
}
}
}
System.out.println("two:"+Arrays.toString(a));
for (int i = 1; i < a.length; i++) {
for (int j=i-1;j>=0;j=j-1){
if (a[j]>a[j+1]){
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
System.out.println("three:"+Arrays.toString(a));*/
int temp = 0;
for (int gap = a.length / 2; gap > 0; gap = gap / 2) {
for (int i = gap; i < a.length; i++) {
for (int j = i - gap; j >= 0; j -= gap) {
if (a[j] > a[j + gap]) {
temp = a[j];
a[j] = a[j + gap];
a[j + gap] = temp;
}
}
}
}
System.out.println(Arrays.toString(a));
}
public void shellSort2(int[] a){//移动法,速度很快,8w个数据一秒钟
for (int gap = a.length / 2; gap > 0; gap = gap / 2) {
for (int i = gap; i < a.length; i++) {
int j=i;
int temp=a[j];
if (a[j]<a[j-gap]){
while (j-gap>=0&&temp<a[j-gap]){
a[j]=a[j-gap];
j-=gap;
}
a[j]=temp;
}
}
}
System.out.println(Arrays.toString(a));
}
}
//shellSort