#include<iostream>
#include<vector>
using namespace std;
vector<int>v;
void Sort(vector<int>&v);
void Adjust(vector<int>&v, int s, int m);
int main()
{
int n, t = 0;
cout << "输入数字总数" << endl;
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> t;
v.push_back(t);
}
Sort(v);
for (int i = 0; i < n; i++)
{
cout << v[i] << " ";
}
system("pause");
return 0;
}
void Sort(vector<int>&v)
{
int n = v.size();
v.insert(v.begin(), 0);
for (int i = n / 2; i > 0; i--)
Adjust(v, i, n);
for (int i = n; i > 1; i--)
{
swap(v[1], v[i]);
Adjust(v, 1, i - 1);
}
v.erase(v.begin());
}
void Adjust(vector<int>&v, int s, int m)
{
int j;
for (j=2*s;j<=m;j*=2)
{
if (j < m&&v[j] < v[j + 1])
j++;
if (v[s] >= v[j])
break;
swap(v[s], v[j]);
s = j;
}
}
堆排序
最新推荐文章于 2024-09-04 23:05:17 发布