希尔排序又称缩小增量排序,是插入排序的改进版本,最坏时间复杂度和平均时间复杂度都是nlog2 n(不一定)
目前并没有找到一种最好的增量序列。
/*希尔排序,选择的序列为9,5,3,2,1
公式为dlta[k]=2^(t-k)+1*/
#include<stdio.h>
const int NUM=10;
void shellInsert(int array[],const int length,int dk){//这里实际上就是一个插入排序
int i,j,tmp;
for(i=dk;i<length;i+=dk){
tmp=array[i];
for(j=i-dk;j>=0&&tmp<array[j];j-=dk)
array[j+dk]=array[j];
array[j+dk]=tmp;
}
}
void shellSort(int array[],const int length){
int dk[5]={9,5,3,2,1};//增量数组
for(int i=0;i<5;i++)
shellInsert(array,length,dk[i]);
}
int main(){
int array[NUM]={1,3,5,7,9,0,8,6,4,2};
shellSort(array,NUM);
for(int i=0;i<NUM;i++){
printf("%d ",array[i]);
}
}
转载请标明出处,原文http://blog.csdn.net/ccy0815ccy