希尔排序增量的问题,引用一本数据结构书中所说:
图示希尔排序思想:
void ShellInsert(int* a, int n, int dk) { int temp; int j, i; for (i = dk; i < n; ++i) { if (a[i] < a[i - dk]) { temp = a[i]; for (j = i; j > 0 && temp < a[j - dk]; j -= dk) a[j] = a[j - dk]; a[j] = temp; } } } void ShellSort(int* a, int n, int* dlta, int t) { for (int i = 0; i < t; ++i) { ShellInsert(a,n,dlta[i]); } } int main() { int arr[] = { 9,2,3,1,5,4,7,8,6 }; int n = sizeof(arr) / sizeof(int); //int dlta[4] = { 5,3,2,1 }; //增量数组 //ShellSort(arr, n,dlta,4); //希尔排序 for (int i = 0; i < n; i++) { cout << arr[i] << " "; } cout << endl; return 0; }