[Sort algorithm] heap sort

Source: Internet
Author: User

Quick sort each time the comparison takes O (n), to make a lgn comparison, the entire sort is finally completed. So the complexity of the fast line is only O (N*LGN). In this section, we are going to talk about heap sorting algorithms. As far as I know, to really thoroughly understand an algorithm, it is best to find the original inventor of this algorithm's paper or related literature.

First, the basic characteristics of the heap sorting algorithm

Time complexity: O (NLGN)//equal to merge sort
Worst: O (NLGN)
Space complexity: O (1)
Not stable.

Establishment of heap and maximal heap

To introduce the heap sorting algorithm, we have to start with the introduction of the heap, then to build the largest heap, and finally the heap sorting algorithm.

2.1 Introduction of the heap

As so:

This is a heap, which can be considered as a complete binary tree, each of which corresponds to an array, and the elements in the heap are stored hierarchically in the array.

Some terms are defined below:

Length[a] Describes the number of elements in an array.
Heap-size[a] Indicates the number of elements in the heap.
These elements are also elements of array A, so heap-size[a]<=length[a].
For the subscript I of a given node, it is easy to calculate the parent node of the node and the subscript of the child's node:

The element value of each parent node in the heap is greater than or equal to its child node (if present), so the heap is the largest heap.

Therefore, the largest element value in the maximum heap appears at the root node (heap top).

From the front, the heap can be considered a tree, so the height of the heap, that is, the height of the tree, O (LGN).
Therefore, the general operating time is O (LGN).

As follows: The Max-heapify:o (LGN)  //This is the key to keeping the maximum heap. The Build-max-heap: linear time. Constructs the largest heap based on an unordered input array. The heapsort://o (NLGN) time, the heap sorting algorithm is to sort an array in situ. The Max-heap-insert, Heap-extract-max, Heap-increase-key, heap-maximum://o (LGN).

Let's take a look at the operation of these functions separately.

2.2 operation to keep the nature of the heap (O (LOGN))

To maintain the nature of the maximum heap, we use the max-heapify operation to make adjustments and recursively invoke this operation so that I is the root of the subtree to become the largest heap.

Max-heapify algorithm as follows (CORE): Max-heapify (A, i) 1 l←left (i) 2 r←right (i) 3 if l≤heap-size[a] and a[l] > A[i] 4 then    Largest←l 5    Else largest←i 6 if r≤heap-size[a] and A[r] > a[largest] 7 then    largest←r 8 if Largest≠i 9 then    Exchange A[i] <-> a[largest]10         max-heapify (A, largest)

As above, the first step is to find the largest one in the corresponding array element A[i], left child a[left (i)], and right child a[right (i)], and store its subscript in largest. If A[i] is already the largest element, the program ends directly. Otherwise, one of the sub-nodes of I is the largest element, which is a[largest] and a[i], so that I and its children can satisfy the maximum heap properties. The value of the element referred to in subscript largest becomes a[i], which violates the maximum heap nature, so max-heapify is called on the element referred to by largest. Below, is the demonstration process of this max-heapify ( is to adjust the 4 to the lowest level, a trip operation, but groping time for Logn):


From, it is easy to see that the initial construction of a maximum heap, after the element A[i], that is, 16, greater than its two sub-nodes 4, 10, to meet the maximum heap properties. So, I downwards points to 4, less than, left sub 14, so, call max-heapify,4 with its sub, 14 swap position. But 4 in 14 of the original position, 4 is less than its right 8, but also violates the maximum heap nature, so recursively call Max-heapify, will 4 and 8, swap position. Thus, the maximum heap properties are satisfied and the program ends.

Run time of Max-heapify

When the max-heapify action is on a tree with a node I as the root, the size is N of the subtree, its run time for the adjustment element A[i], a[left (i)],a[right (i)] The relationship time is O (1), Plus, The time required to call max-heapify on a subtree with one of the child nodes of I, and the subtree size of the I node is at most 2N/3, so the max-heapify run time is
T (n) ≤t (2N/3) +θ (1).

We can testify that the recursive solution of this equation is T (n) =o (LGN)

2.3 Build Heap O (N)
Build-max-heap (a) 1  heap-size[a]←length[a]2  for i← (LENGTH[A]/2) Downto       to do max-heapify (A, I)    //Build heap, How to build it? The original is to constantly call max-heapify (A, I) to build the largest heap.

Build-max-heap invokes a max-heapify on every other node,
To create a maximum heap corresponding to the array A[1...N]. The elements in a[(|_n/2_|+1) ‥n] are leaves in the tree.
So, naturally, each node can be thought of as a heap with only one element.

For the correctness of this process build-max-heap (A), refer to chapter 6th of the introduction to the algorithm, section 6.3.
, is an example of this process ( is the constant invocation of the max-heapify operation, all the violation of the heap nature of the number must be adjusted, a total of N -pass operation, and then the groping time is finally accurate O (N)):


third, heap sorting algorithm

The so-called heap ordering, is called the two processes: a heap of operations, BUILD-MAX-HEAP, one to maintain the maximum heap of operations, max-heapify. The detailed algorithm is as follows:

Heapsort (a)    //n-1 call max-heapify, So, O (N*LGN) 1 build-max-heap (a)      //Built Max Heap, O (n) 2 for I←length[a] Downto    Do Exchange a[1] <-> a[i]4       heap-size[a]←heap-size[a]-       max-heapify (A, 1)    //Preserve the nature of the heap, O (LGN)

As above, that is the complete description of the heap sorting algorithm. Below, and then paste the above heap sorting algorithm in the two heap, and maintain the maximum heap operation:

Build-max-heap (a)  //Build heap 1  heap-size[a]←length[a]2  for i←|_length[a]/2_| downto       -Do max-heapify (A, I ) max-heapify (A, i)     //Hold Maximum heap 1 l←left (i) 2 r←right (i) 3 if l≤heap-size[a] and a[l] > A[i] 4 then    largest ←l 5    Else largest←i 6 if r≤heap-size[a] and A[r] > a[largest] 7 then    largest←r 8 if Largest≠i 9
    then Exchange A[i] <-> a[largest]10         

The following is the demonstration process of the heap sorting algorithm (through the constant exchange of the largest element of the top and the last element, and the constant invocation of max-heapify to re-maintain the nature of the maximum heap, and finally, one, from large to small, to clean up all the elements in the heap, and to form an orderly sequence. This is the whole process of heap sequencing. ):

, A->b,b->c, .... Between, there is a top-most element and the smallest element after the exchange, call max-heapify process, we know that this max-heapify run time is O (LGN), and to complete the entire heap sequencing process, the total must go through the O (n) such max-heapify operations. Therefore, the run time of the heap sorting algorithm is O (N*LGN).

follow up: think of the heap as a tree, a binary tree, etc. Therefore, the time complexity of finding and deleting data in Heaps is O (logn). So what kind of a two-fork tree column? A special two-fork tree, divided into the largest heap, the smallest heap. The biggest heap, is the top big, the head small. The smallest heap is the top small, the head big.

[Sort algorithm] heap sort

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.