堆排序,以及stl中的堆应用
2014-05-02 16:31
232 查看
堆,由数组实现的一种完全二叉树(在计算倒数第一个父节点用到了!)的一种算法。
大堆,保证根节点的值大于左右节点值。常用于排序,求最小的k个值(k<n)。
小堆,保证根节点的值小于左右节点值。求最大的k个值(k<n)。
在实现堆时,注意下标,此处是从0,开始。
父节点为为 i,子节点为2i+1,2i+2;
子节点为i,父节点(i-1)/2
第一个叶子节点的前一个点为(n- 2)/2。(因为所有父节点数为n/2,下标即为n/2 - 1)
实现了top k 问题的最小堆,最大的几个值。
#include <iostream>
#include <algorithm>
#include <functional>
#include <cassert>
using namespace std;
void verify_heap();
void top_k(int *array,int n,int k);
void max_top_k(int *array,int n,int k);//获取最小的k个数
int main()
{
int array[9] = {4,3,7,2,1,6,5,9,8};
//verify_heap();
top_k(array,9,4);
max_top_k(array,9,3);
cout<<"small_top_k: ";
for (int k = 0;k < 3;k++)
{
cout<<array[k]<<" ";
}
cout<<endl;
return 0;
}
void verify_heap()
{
int array[9] = {4,3,7,2,1,6,5,9,8};
make_heap(array,array+9);
//make_heap(array,array+9,greater<int>());//小堆
for (int i = 0;i < 9;i++)
{
cout<<array[i]<<" ";
}
cout <<endl;
/*
for (int j = 0;j < 9;j++)
{
pop_heap(array,array+9-j);
cout<<array[9-j-1]<<" ";
}
cout <<endl;
*/
sort_heap(array,array+9);
for (int k = 0;k < 9;k++)
{
cout<<array[k]<<" ";
}
cout <<endl;
}
void top_k(int *array,int n,int k)
{
assert(array);
make_heap(array,array+n,greater<int>());
int k_deal = k%n;
cout<<"min_top_k:";
for (int i = 0;i < k_deal;i++)
{
pop_heap(array,array+n-i,greater<int>());//每一个都得加上greater<int>()
cout<<array[n-1-i]<<" ";
}
cout<<endl;
}
void max_top_k(int *array,int n,int k)//获取最大的k个数
{
assert(k < n);
make_heap(array,array+k,greater<int>());
for (int i = k; i < n;i++)//注意当心下标问题影响到heap
{
if (array[i] > array[0])
{
swap(array[i],array[0]);
make_heap(array,array+k,greater<int>());
}
}
}
大堆,保证根节点的值大于左右节点值。常用于排序,求最小的k个值(k<n)。
小堆,保证根节点的值小于左右节点值。求最大的k个值(k<n)。
在实现堆时,注意下标,此处是从0,开始。
父节点为为 i,子节点为2i+1,2i+2;
子节点为i,父节点(i-1)/2
第一个叶子节点的前一个点为(n- 2)/2。(因为所有父节点数为n/2,下标即为n/2 - 1)
#include <iostream> #include <algorithm> #include <functional> #include <cassert> using namespace std; void recursive_max_heap_shift_down(int *array,int start,int end); void make_max_heap(int *array,int start,int end); void nonrecursive_make_max_heap(int *array,int start,int end); void max_heap_shift_down(int *array,int start,int end);//下溯 void test_nonrecursive_max_heap(int *array,int n); void heap_sort(int *array,int n); int main() { int array[9] = {4,3,7,2,1,6,5,9,8}; cout<<"make_max_heap"<<endl; make_max_heap(array,0,9); for(int k = 0;k < 9;k++) { cout<<array[k]<<" "; } cout<<endl; for (int j = 9;j >1;j--) { swap(array[0],array[j-1]); recursive_max_heap_shift_down(array,0,j-1);//原先问题出在这里,不小心写成了j //即从第8个开始 cout<<"heap temp result:"; for(int k = 0;k < j-1;k++) { cout<<array[k]<<" "; } cout<<endl; } cout<<"sort:"; for(int k = 0;k < 9;k++) { cout<<array[k]<<" "; } cout<<endl; /* for(int k = 1;k < 9;k++) { make_max_heap(array,k,8); cout<<array[k]<<endl; } */ int array_second[9] = {4,3,7,2,1,6,5,9,8}; test_nonrecursive_max_heap(array_second,9); return 0; } //递归的方式,区间为[),若想区间为[]则只需更改条件,<end; void recursive_max_heap_shift_down(int *array,int start,int end)//下溯 { int left_index = start*2+1; int right_index = start*2+2; int largest = start;//大的节点索引,当做下一个父节点! if (left_index < end && array[left_index] > array[start]) { largest = left_index ; } else { largest = start ; } if (right_index < end &&array[right_index] > array[largest])//大于大的(或者先比较左右大的,在于父节点比较) { largest = right_index; } if(largest != start) //不相等则交换,且继续! { swap(array[largest],array[start]); //cout<<"array[largest]:"<<array[start]<<endl; recursive_max_heap_shift_down(array,largest,end); } } void make_max_heap(int *array,int start,int end) { int len = end - start ; int parent = start + (len - 2)/2;//第一个父节点 for (int i = parent; i >= start;i--)//包括start,所有父节点都得调整 { recursive_max_heap_shift_down(array,i,end);//父节点i } } //非递归版本 void max_heap_shift_down(int *array,int start,int end)//下溯 { int left_index = start*2+1; int right_index = start*2+2; int largest = start;//大的节点索引,当做下一个父节点! while(left_index < end) { if (array[left_index] > array[start]) { largest = left_index ; } else { largest = start ;//作为当前大的 } if (right_index < end &&array[right_index] > array[largest]) { largest = right_index; } if(largest != start) //不相等则交换,且继续! { swap(array[largest],array[start]); //更新 start = largest; left_index = 2*start + 1; right_index = left_index + 1; } else { break; } } } void nonrecursive_make_max_heap(int *array,int start,int end) { int len = end - start ; int parent = start + (len - 2)/2;//第一个父节点 for (int i = parent; i >= start;i--)//包括start,所有父节点都得调整 { max_heap_shift_down(array,i,end);//父节点i } } void heap_sort(int *array,int n) { nonrecursive_make_max_heap(array,0,n);//先建堆,在一个个把堆顶放到最后 for(int i = n-1;i >0;i--) { swap(array[0],array[i]);//放置最后 max_heap_shift_down(array,0,i); } } void test_nonrecursive_max_heap(int *array,int n) { cout<<"original array:"; for (int i = 0; i < n;i++) { cout<<array[i]<<" "; } cout<<endl; /* nonrecursive_make_max_heap(array,0,n); cout<<"max_heap array:"; for (int j = 0; j < n;j++) { cout<<array[j]<<" "; } cout<<endl; for (int k = n-1;k >= 1 ;k--) { swap(array[0],array[k]); max_heap_shift_down(array,0,k);//下一个进行回溯 } cout<<"sort:"; for(int m = 0;m < n;m++) { cout<<array[m]<<" "; } cout<<endl; */ cout<<"heap_sort:"; heap_sort(array,n); for(int m = 0;m < n;m++) { cout<<array[m]<<" "; } cout<<endl; }//参见侯捷的书,以及http://www.cplusplus.com/reference/algorithm/make_heap/?kw=make_heap
实现了top k 问题的最小堆,最大的几个值。
#include <iostream>
#include <algorithm>
#include <functional>
#include <cassert>
using namespace std;
void verify_heap();
void top_k(int *array,int n,int k);
void max_top_k(int *array,int n,int k);//获取最小的k个数
int main()
{
int array[9] = {4,3,7,2,1,6,5,9,8};
//verify_heap();
top_k(array,9,4);
max_top_k(array,9,3);
cout<<"small_top_k: ";
for (int k = 0;k < 3;k++)
{
cout<<array[k]<<" ";
}
cout<<endl;
return 0;
}
void verify_heap()
{
int array[9] = {4,3,7,2,1,6,5,9,8};
make_heap(array,array+9);
//make_heap(array,array+9,greater<int>());//小堆
for (int i = 0;i < 9;i++)
{
cout<<array[i]<<" ";
}
cout <<endl;
/*
for (int j = 0;j < 9;j++)
{
pop_heap(array,array+9-j);
cout<<array[9-j-1]<<" ";
}
cout <<endl;
*/
sort_heap(array,array+9);
for (int k = 0;k < 9;k++)
{
cout<<array[k]<<" ";
}
cout <<endl;
}
void top_k(int *array,int n,int k)
{
assert(array);
make_heap(array,array+n,greater<int>());
int k_deal = k%n;
cout<<"min_top_k:";
for (int i = 0;i < k_deal;i++)
{
pop_heap(array,array+n-i,greater<int>());//每一个都得加上greater<int>()
cout<<array[n-1-i]<<" ";
}
cout<<endl;
}
void max_top_k(int *array,int n,int k)//获取最大的k个数
{
assert(k < n);
make_heap(array,array+k,greater<int>());
for (int i = k; i < n;i++)//注意当心下标问题影响到heap
{
if (array[i] > array[0])
{
swap(array[i],array[0]);
make_heap(array,array+k,greater<int>());
}
}
}
相关文章推荐
- 【C++ STL应用与实现】26: 如何使用std::for_each以及基于范围的for循环 (since C++11)
- 【STL】栈的实现原理以及应用
- 快速排序以及堆排序在查找数方面的主要应用 即将引出线性查找BFPRT算法
- 【STL】队列的实现以及应用
- 堆的创建与应用以及堆排序
- Android深入浅出系列之实例应用—简单的手指拖动图片,图片滑来滑去显示应用Gallery和BaseAdapter以及ImageView的使用
- 数据库优化-垂直切分以及在实际项目中的应用
- ssh开发web应用中各个框架的作用以及整合配置步骤
- img标签的onerror事件(应用以及注意事项)
- 建堆,以及对堆排序
- 【堆】二分堆的实现以及STL中的堆
- 【转】委托与事件以及应用
- 应用STL做的撤销重做封装
- 使用Android NDK以及JNI编写应用
- HDU 2072.单词数【STL的优势以及字符串流的使用】【8月4】
- .Net语言 APP开发平台——Smobiler学习日志:Poplist控件在APP中的应用场景以及代码
- 自己对IT支撑系统的了解以及实际应用
- [置顶] Yolo的搭建和在Windows下封装以及工程应用
- STL中的stack的应用
- 通过jar包和url调取第三方地图应用以及自动导航