Priority queue
The priority queue is a collection of 0 or more elements, each of which has a priority, the operations performed on the priority queue are searched, a new element is inserted, and the deletion is done. In general, a find operation is used to search for the element with the highest priority, and the delete operation is used to delete the element. For elements with the same priority, they can be processed in FIFO order or at any priority.
Below we use the heap to implement the priority queue ( this will no longer implement the heap, the implementation of the heap see a blog post )
Template<class T>class Pariorityqueue{public:pariorityqueue () {} void Push (const t& x)//o (LgN) {_h.push (x); } void Pop () {_h.pop (); }protected:heap<t> _h;};
2. Find the maximum number of top K (n>m) in the N number
Idea: first read K to build a small heap, each read a comparison with the heap, if large let heap top//With this number exchange, and then do a downward adjustment.
#pragma once#include<iostream> #include <time.h> #include <assert.h>using namespace std;const int n = 10000;const int k = 100;void adjustdown ( int topk[],int size,int parent) //Build a small heap { int child = 2 * parent + 1; while (child < size) { if (child + 1 < size ) && (topk[child] > topk[child + 1]) { child++; } if (topk[ Parent] > topk[child]) { swap (Topk[child], topk[parent]); parent = child; child = 2 * parent + 1; } else { break; &NBSP;&NBSP;}&NBSP;&NBSP;&NBSP;&NBSP;}}VOID&NBSP;GETK (int a[]) { assert (K < n); int topk[k]; for (int i = 0; i < k; ++i) { topk[i] = a[i]; } //Build Heap for (INT&NBSP;I&NBSP;=&NBSP; (k - 2) / 2; i >= 0; --i) { adjustdown (topk,k,i); } for (int i = k; i < n; ++i) { if (a[i]>topk[0]) { topk[0] = a[i]; adjustdown (topk, k, 0); } } for (int i = 0; i < k; ++i) { cout << topk[i] << " "; }}void test2 () { &nbsP;srand (Time (0)); int a[n] = { 0 }; for (int i = 0; i < n; ++i) { a[i] = rand (); } GETK (a);}
3. Heap sort.
Train of thought: J Build a big pile, the top element of the heap to exchange to the end,//the remaining data downward adjustment #pragma once#include<iostream> #include <assert.h>using Namespace std;void adjustdown (int a[], int size, int parent) { int child = 2 * parent + 1; while ( Child < size) { if (( child + 1 < size) && (a[child] < a[child + 1]) ) { child++; } if (A[parent] < a[child]) { swap (A[child], a[parent]); parent = child; child = 2 * parent + 1; } else { break; } }}void heapsort (int a[],int size) { assert (a); // for (int i = (size - 2) / 2; i >= 0; --i) { adjustdown (a,size,i); } for (int i = 0; i < size; i++) { &nbsP; swap (A[0], a[size - 1 - i]); adjustdown (a, size - 1 - i, 0); } }void test3 () { int a[] = { 2, 6, 3, 8, 49, 95, 1, 5, 8 }; int size = sizeof (a) / sizeof (a[0]); heapsort (a, size); for (int i = 0; i < size; i++) { cout << a[i] << " "; }
The results are as follows:
650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/7F/F4/wKioL1cy1LiyrIX2AAAr_D8m0eI491.png "title=" capture. PNG "alt=" Wkiol1cy1liyrix2aaar_d8m0ei491.png "/>
This article is from the "Shuoyuexinmao Cloud" blog, please be sure to keep this source http://19940325.blog.51cto.com/10789287/1772191
Application of the heap