Algorithm 2.4--priority queue and heap ordering

Source: Internet
Author: User
Tags comparable

The use of priority Queues is similar to the queue (deleting the oldest element) and the stack (deleting the newest element). To give a practical example: that is, from 1 billion elements to choose the largest 10, with a priority queue, you only need a can store 10 elements of the queue.

And the binary heap is good to implement the basic operation of the priority queue. The two-fork heap is a set of elements that can be sorted with the complete binary tree of the teammate Xu. Among the algorithms about the heap, there are floating swim and sinking sink, in addition generally speaking a[0] not used, directly using a[1].

Implementation of Heap sorting

The implementation of heap sequencing requires two issues to be resolved:

1. How to build a heap from an unordered sequence?

2. How do I adjust the remaining elements to become a new heap after the top element of the output heap?  

The second problem, usually after the output heap top element, is considered to exclude this element, then fill it with the last element of the table, and adjust it from top to bottom: first, the top element of the heap is compared to the root node of its left and right subtree, the smallest element is exchanged to the top of the heap, and then the broken path is adjusted. Until the leaves knot, you get a new heap.

We call this "screening" the adjustment process from the top of the heap to the leaves.

The process of building a heap from an unordered sequence is a process of "filtering" over and over again.

Instance procedure:

Build the initial heap structure

Then, swap the elements of the heap top and the last element, at which point the last position as an ordered area (the ordered area is shown in yellow), and then the heap adjustment of the other unordered areas, after the large top heap, swap the top of the heap and the position of the penultimate element:

Repeat these steps:

Code implementation (Java):


public class Heap {


Private Heap () {}

public static void sort (comparable[] pq) {
int N = Pq.length;
for (int k = N/2; k >= 1; k--)//Construction Heap
Sink (PQ, K, N);
while (N > 1) {//Swap repair Heap
Exch (PQ, 1, n--);
Sink (PQ, 1, N);
}
}


private static void Sink (comparable[] PQ, int k, int N) {
while (2*k <= N) {
int j = 2*k;
if (J < N && Less (PQ, J, J+1) J + +;
if (!less (PQ, K, J)) break;
Exch (PQ, K, J);
K = J;
}
}


private static void Exch (object[] PQ, int i, int j) {
Object swap = pq[i-1];
PQ[I-1] = pq[j-1];
PQ[J-1] = swap;
}

Heapsort Algorithm Analysis:
Heap sorting method is not worth advocating for files with fewer records, but it is still very effective for n larger files. Because its run time is mainly spent on the initial heap under construction and the adjustment of the new heap when the repeated "filtering".

Heap sequencing in the worst case, the time complexity is also O (NLOGN). This is the greatest advantage of heap sorting relative to fast sorting. In addition, heap ordering requires only one record-size secondary storage space for Exchange.

Algorithm 2.4--priority queue and heap ordering

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.