#include<iostream>
#include<ctime>
uing namespace std;
/*
希尔排序是直接插入排序的改进
*/
void swap(int *arr, int i, int k) { int temp = arr[i]; arr[i] = arr[k]; arr[k] = temp; } void ShellSort(int *arr, int length) { int flag = length / 2; while (flag) { for (int i = flag; i < length; i++) { int temp = arr[i]; while (i - flag >= 0) { if (arr[i] > arr[i - flag]) break; else { swap(arr, i, i - flag); i = i - flag; }
} } flag = flag / 2; } } int main() { srand(time(nullptr));
int nArr[5]; for (int i = 0; i < 5; i++) nArr[i] = rand() % 100; cout << "排序后:" << endl; ShellSort(nArr, 5); for (int i = 0; i < 5; i++) cout << nArr[i] << endl;
return 0;
}