Sorting algorithm heap sort (priority queue)

Source: Internet
Author: User

1, heap sort of heap, in fact, is a complete two-pronged tree. is either a node or a leaf node, or there must be a tree with about two sub-nodes.

2, heap Order: The value of each node must be greater than two child nodes. However, the size of the two sub-nodes is not required.

3, a complete binary tree size n, height of lgn (layer).

Using an array to implement the heap, assuming that the array subscript starting from 0, subscript the element K, its Zuozi is 2k+1, the right subtree is the left subtree +1, namely 2k+2

One: Ordered from top to bottom (sinking)

If the heap is in an orderly state, because a node is broken than its two sub-nodes or one of them, it can be swapped with the larger of the two sub-nodes.

After the exchange may break the original order state at the Sub-node, then only need to continue sinking until the leaf node.

Because it is exchanged in a subtree, it has no effect on the other branches of the tree.

Sinking maintenance heap public static void sink (Int[]a,int k,int N) {while (2*k+1<=n) {int J = 2*k+1;//Left child subscript//j<n, indicating existence right child//a[j]< A[j+1], left child small less right operand child if (j<n&&a[j]<a[j+1]) j++;//If the root node is not smaller than his child, do not sink if (! ( A[K]<A[J]) break;//otherwise example.exch (A, K, j);//exchange, change current, continue sinking k=j;} End While}//end Sink

Two: Sinking implemented heap sort (in situ sort)

1. Build the heap

For an ordinary array, we can treat him as an unordered heap.

To use heap ordering, the first step is to construct the array into an ordered heap.

A simple idea is to create a new array, through the method of inserting elements of the heap, a single element that inserts the original array, and then a new heap is completed.

Actually do not need, we just put the former N/2 elements, sink to the appropriate position, when the heap is orderly, after the n/2 are all leaf nodes, without sinking.

2. Sinking sort

By definition, a root node is a 0-bit element of an array, which is always the maximum value of an ordered heap.

So we just need to swap it and the last element of the array, and then the size of N-1 (minus the last element, because he is the maximum value, put it at the end), and build it from the new heap. How to build it? is to sink the new No. 0 element into its proper position.

In this way, an ordered array can be obtained.

Sinking sort public static void sort (int []a) {//Build heap//start can treat an array as unordered heap//will sink the element from subscript N/2 to 0 to the appropriate position. The elements behind the n/2 are actually leaf nodes that do not have to sink. int N = a.length-1;for (int k = n/2;k>=0;k--) {sink (a,k,n);} Sinking sort//the root node of the heap is always the maximum, we simply swap the maximum and the last element for the position. Then maintain a heap other than the original maximum node, and then place the root node of the new heap in the penultimate position of the N-1. So repeatedly while (n>0) {example.exch (A, 0, n--); sink (a,0,n);}} End Sort

Complexity of Time:

The main is sinking, in fact, the time to sink the complexity is very intuitive, each sinking, the height of the node-1, the height of a tree is LGN, so from the No. 0 bit sinking to the last (worst case), but also just LGN.

In the following we sank in the sort of each element in fact, so the overall is NLGN.

Complexity of space:

In this algorithm, we do not use temporary variables or new arrays, so we do not need extra space. Complexity is constant.


Not stable

Sorting algorithm heap sort (priority queue)

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: 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.