#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main()
{
vector<int> ivec;
for (int i = 4; i <= 8; ++i)
ivec.push_back(i);
for (int i = 3; i <= 7; ++i)
ivec.push_back(i);
for (int i = 1; i <= 5; ++i)
ivec.push_back(i);
for (vector<int>::iterator iter = ivec.begin(); iter != ivec.end(); ++iter)
cout << *iter << ' ';
cout << endl;
make_heap(ivec.begin(), ivec.end()); //make_heap是堆排序算法,
for (vector<int>::iterator iter = ivec.begin(); iter != ivec.end(); ++iter)
cout << *iter << ' ';
cout << endl;
cout << endl;
pop_heap(ivec.begin(), ivec.end()); // pop_heap()是把最大的那个数放到最后边,重新生成一个堆,
for (vector<int>::iterator iter = ivec.begin(); iter != ivec.end(); ++iter)
cout << *iter << ' ';
cout << endl;
cout << endl;
ivec.pop_back(); // 把向量中的最后一个数据删除,
for (vector<int>::iterator iter = ivec.begin(); iter != ivec.end(); ++iter)
cout << *iter << ' ';
cout << endl;
cout << endl;
ivec.push_back(10); // 将10加入到向量中保存在最后面,
for (vector<int>::iterator iter = ivec.begin(); iter != ivec.end(); ++iter)
cout << *iter << ' ';
cout << endl;
cout << endl;
push_heap(ivec.begin(), ivec.end()); // push_heap()是加入一个新的数据得到新的堆,
for (vector<int>::iterator iter = ivec.begin(); iter != ivec.end(); ++iter)
cout << *iter << ' ';
cout << endl;
cout << endl;
sort_heap(ivec.begin(), ivec.end()); // sort_heap 把堆变成不是堆,只是一个排序,
for (vector<int>::iterator iter = ivec.begin(); iter != ivec.end(); ++iter)
cout << *iter << ' ';
cout << endl;
cout << endl;
return 0;
}
堆排序算法
最新推荐文章于 2024-04-28 11:24:41 发布