希尔排序使用插入排序对间隔为N的序列进行排序,并不断减小间隔,最后间隔为1,就使得整个数组是有序的。
cpp:
#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>
void shellsort(std::vector<int> &nums)
{
int N = nums.size();
int k = 0;
for (int Increment = N; Increment > 0; Increment /= 2)
{
// insertsort
for (int i = Increment; i < N; i++)
{
int temp = nums[i];
int j;
for (j = i - Increment; j >= 0; j -= Increment)
{
if (nums[j] > temp)
{
nums[j + Increment] = nums[j];
// std::cout << j;
}
else
{
break;
}
}
nums[j + Increment] = temp;
std::cout <