#include<iostream>
#include<vector>
using namespace std;
vector<int>v;
void Hill(vector<int>&v);
int main()
{
int n, t = 0;
cout << "输入数字总数" << endl;
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> t;
v.push_back(t);
}
Hill(v);
for (int i = 0; i < n; i++)
{
cout << v[i] << " ";
}
system("pause");
return 0;
}
void Hill(vector<int>&v)
{
int step = v.size();
int e, j;
while (step > 1)
{
step = step / 3 + 1;
for (int i = step; i < v.size(); i++)
{
if (v[i] < v[i - step])
{
e = v[i];
for (j = i - step; j >= 0 && v[j] > e; j -= step)//此处是插入排序
{
v[j + step] = v[j];
}
v[j + step] = e;
}
}
}
}
希尔排序
最新推荐文章于 2023-11-28 16:53:17 发布