排序-------希爾排序
阿新 • • 發佈:2020-12-10
#include<stdio.h>
void ShellInsert(int* L, int dk,int nL) {
int i, j;
for(i=dk+1;i<nL;++i)
if (L[i]< L[i - dk]) {
L[0] = L[i];
for (j = i - dk; j > 0 && (L[0]< L[j]); j -= dk)
L[j + dk] = L[j];
L[j + dk] = L[0];
}
}
void ShellSort(int* L, int * dlta, int t, int nL) {
int k;
for (k = 0; k < t; ++k) {
ShellInsert(L, dlta[k],nL);
printf("第%d次: ", k + 1);
for (int i = 1; i < 11; i++) {
printf("%3d ", L[i]);
}
printf("\n");
}
}
int main() {
int L[11] = {0, 49,38,65,97,76,13,27,49,55,04 };
int dlta[4] = { 5,3,2,1 };
printf("原資料:");
for (int i = 1; i < 11; i++) {
printf("%3d ", L[i]);
}
printf("\n");
ShellSort(L, dlta, 4, 11);
return 0;
}
執行結果: