希尔排序ShellSort
基本思想:
在要排序的一组数中,根据某一增量分为若干子序列,并对子序列分别进行插入排序。
然后逐渐将增量减小,并重复上述过程。直至增量为1,此时数据序列基本有序,最后进行插入排序。
过程:一开始设置增量是数长度的一半,每隔增量的数据为一组进行插入排序。
c++代码实现如下:
void ShellSort(int a[],int len){
int temp;
for(int i=len/2;i>0;i/=2){
for(int j=i;j<len;j++){
int k=j;
while (k-i>=0&&a[k]<a[k-i])
{
temp=a[k];
a[k]=a[k-i];
a[k-i]=temp;
k-=i;
}
}
for(int i=0;i<len;i++)
cout<<a[i]<<" ";
cout<<endl;
}
}
int main(void){
int a[]={42,20,17,13,28,14,23,15};
cout<<"长度;"<<sizeof(a)/sizeof(int)<<endl;
//BubbleSort(a,8);
//SelectSort(a,8);
//InsertSort(a,8);
ShellSort(a,8);
system("pause");
return 0;
}
结果如下:
https://www.runoob.com/w3cnote/sort-algorithm-summary.html