随着增量逐渐减少,每组包含的关键词越来越多,当增量减至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;
}
}