结构体-heap排序

来源:互联网 发布:java getstacktrace 编辑:程序博客网 时间:2024/05/08 06:29

头文件

#include <algorithm>

make_heap(start,end,function)       [start,end)

pop_heap(start,end,function)          [start,end)->[start,end-1) 根->end-1

push_heap(start,end,function)        [start,end-1)->[start,end)调整堆

heap_sort(start,end,function)          [start,end) 堆排


// edit by colin// 20141104// usage of heap in stl#include <iostream>#include <algorithm>using namespace std;struct test{    int a;    int b;    int c;}tt[11];bool comp(const test &one,const test &two){    return one.c < two.c;}int main(){    for(int i=0;i<11;++i)        tt[i].c = i+1;    srand((unsigned)time(NULL));    random_shuffle(&tt[0],&tt[11]);    for(int i=0;i<11;++i)        cout<<tt[i].c<<" ";    cout<<endl;    make_heap(&tt[0],&tt[10],comp);    for(int i=0;i<11;++i)        cout<<tt[i].c<<" ";    cout<<endl;    //pop_heap(&tt[0],&tt[10],comp);    push_heap(&tt[0],&tt[11],comp);    for(int i=0;i<11;++i)        cout<<tt[i].c<<" ";    cout<<endl;    sort_heap(&tt[0],&tt[11],comp);    for(int i=0;i<11;++i)        cout<<tt[i].c<<" ";    cout<<endl;    return 0;}


0 0
原创粉丝点击