Chapter 6 "maximum priority queue" in heap sorting"

Source: Internet
Author: User

Use the largest heap to implement the maximum priority queue:

// Returns the maximum value of the heap.
Int heapmaximum (int *)
// Remove and return the element with the maximum keyword in the heap
Int heapextractmax (int * a, int * heapsize)
// Add the key value of element x to K. Here, K cannot be smaller than the original key value of X.
Void heapincreasekey (int * a, int I, int K)
// Insert element x into the heap
Void maxheapinsert (int * a, int * heapsize, int key)

# Include <stdio. h> # include <string. h> # include <time. h> # define buffer_size 10 // heap adjustment to maintain the heap nature void maxheapify (int * a, int I, int heapsize) {int left = I; int right = I; int TMP; int largest = I; while (I <= heapsize) {left = I <1; // left sub-index right = (I <1) + 1; // (the shift operation has a lower priority than the arithmetic operation !) Right sub-index largest = I; If (left <= heapsize & A [I] <A [left]) {largest = left ;} if (right <= heapsize & A [Largest] <A [right]) {largest = right;} if (I! = Largest) // an endless loop may occur if you do not add this judgment. In this case, you can compress {TMP = A [I]; A [I] = A [Largest]; A [Largest] = TMP; I = largest; // actually, it depends on the amount of recursion changes each time to eliminate recursion} else {break ;}}} // heap creation void buildmaxheap (int * a, int heapsize) {int I = 0; for (I = buffer_size/2; I> 0; I --) {maxheapify (, i, heapsize) ;}}// returns the maximum heap value int heapmaximum (int * A) {return a [1];} // remove and return the int heapextractmax (int * a, int * heapsize) element with the maximum keyword in the heap {int max = 0; int n = * heapsize; if (n <1) {return-1;} max = A [1]; A [1] = A [n ]; * Heapsize = n-1; maxheapify (A, 1, * heapsize); Return Max;} // increase the key value of element x to K, here, K cannot be less than the original keyword value of X void heapincreasekey (int * a, int I, int K) {int TMP = 0; If (k <A [I]) {return;} A [I] = K; while (I> 1 & A [I]> A [I> 1]) {TMP = A [I]; A [I] = A [I> 1]; A [I> 1] = TMP; I = I> 1 ;}} // insert element x into the heap void maxheapinsert (int * a, int * heapsize, int key) {* heapsize + = 1; A [* heapsize] = key-1; // assign the newly added element a value smaller than the key heapincreasekey (A, * heapsize, key);} void output (int *, Int Len) {int I = 1; for (I = 1; I <= Len; I ++) {printf ("% d", a [I]);} printf ("\ n") ;}int main () {int I = 0; int heapsize = 0; int A [buffer_size + 1]; // The first position is a [0]. Therefore, it is easy to calculate the index of a subnode. Memset (A, 0, sizeof (a); srand (unsigned) Time (null); for (I = 1; I <= buffer_size; I ++) {A [I] = rand () % buffer_size;} printf ("randomly generated array:"); output (A, buffer_size); // create the maximum heap buildmaxheap (, buffer_size); printf ("Maximum heap created:"); output (A, buffer_size); // return the maximum keyword element in the heap printf ("return the maximum keyword element in the heap: % d \ n ", heapmaximum (a); // remove and return the element heapsize = buffer_size with the maximum keyword in the heap; printf ("Remove and return the element with the maximum keyword in the heap: % d \ n", heapextractmax (A, & heapsize )); // Add the key value of element x to K. Here, K cannot be smaller than the original key value of X heapincreasekey (A, 4, buffer_size * 2 ); printf ("add the keyword value of element a [4] To buffer_size * 2:"); output (A, heapsize ); // insert element x into the heap maxheapinsert (A, & heapsize, buffer_size * 3); printf ("insert element buffer_size * 3 into the heap :"); output (A, heapsize); System ("pause"); Return 0 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.