#pragma once
void ShellSort(int* array, size_t n)
{
int gap = n;
while (gap > 1)
{
gap = gap/3 + 1;
for (size_t i = 0; i < n-gap; ++i)
{
int end = i;
int tmp = array[end+gap];
while ((end >= 0) && array[end] > tmp)
{
array[end+gap] = array[end];
end -= gap;
}
array[end+gap] = tmp;
}
}
}
void ShellSortTest()
{
int array[] = {2, 4, 6, 5, 8, 3, 7, 1, 0, 9};
InsertSort(array, sizeof(array)/sizeof(array[0]));
for (size_t i = 0; i < sizeof(array)/sizeof(array[0]); ++i)
{
cout<<array[i]<<" ";
}
cout<<endl;
}#include <iostream>
using namespace std;
#include "ShellSort.h"
int main()
{
ShellSortTest();
return 0;
}本文出自 “zgw285763054” 博客,请务必保留此出处http://zgw285763054.blog.51cto.com/11591804/1787204
原文地址:http://zgw285763054.blog.51cto.com/11591804/1787204