希尔排序

来源:互联网 发布:知己知彼软件破解版 编辑:程序博客网 时间:2024/05/16 04:56
该方法实质上是一种分组插入方法。把记录按下标的一定增量分组,对每组使用直接插入排序算法排序;

随着增量逐渐减少,每组包含的关键词越来越多,当增量减至1时,整个文件恰被分成一组,算法便终止。

void insertSort(int *data,int count,int startIndex,int step);void shellSort(int *data,int count);unsigned int getMin2Pow(int count);unsigned int getMin2Pow(int count) {unsigned int tmp;unsigned int result = (count >> 1);for(tmp = result >> 1;tmp > 0;tmp >>= 1){result |= tmp;}return result;}void shellSort(int *data,int count) {int step;int startIndex;for(step = getMin2Pow(count);step > 0;step >>= 1){for(startIndex = 0;startIndex < step;startIndex++) {insertSort(data,count,startIndex,step);}}}void insertInsert(int *data,int count,int startIndex,int step) {int i;int j;int t;int temp;for(i = startIndex + step;i < count;i += step) {temp = data[i];for(j = startIndex;j < i && data[j] <= data[i];j += step);for(t = i - step;t >= j;t -= step) {data[t+step] = data[t];}data[j] = temp;}}


0 0
原创粉丝点击