Sorting Algorithm for algorithm learning: heap sorting

Source: Internet
Author: User

Sorting Algorithm for algorithm learning: heap sorting

To understand heap sorting, first understand the concept of heap, because this article mainly studies the heap sorting algorithm. Here we only give the concept of the data structure heap: the sequence of n elements {k1, k2 ,... kn}. It is called heap only when the following relationship is satisfied.

K [I] <= k [2i] and k [I] <= k [2i + 1] (or k [I]> = k [2i] and k [I]> = k [2i + 1])

For example, sequences 96, 83, 27, 38, 11, 09 (or 12, 36, 24, 85, 47, 30, 53, and 91) are all heap.

If we regard the corresponding one-dimensional array as a binary tree, the meaning of the heap indicates that the values of all non-terminal nodes in the Complete Binary Tree are not greater than (or not less) the value of the left and right child nodes. Rewrite the heap in the one-dimensional array form to a binary tree form as follows:

96 12

/\/\

83 27 36 24

/\//\/\

38 11 9 85 47 30 53

/

91

The heap and binary tree heap examples of the one-dimensional array given above show that if the sequence {k1, k2 ,... kn} is a heap, and the top element (or the root of a full Binary Tree) of the heap must be the minimum (or maximum) of n elements in the sequence ).


Heap sorting process: if the minimum value of the output heap top is equal to or equal to one heap after the sequence weight of the remaining n-1 elements, the sub-smaller value of n elements is obtained. Run the command repeatedly to obtain an ordered sequence.


Two problems need to be solved to achieve heap sorting:

1. Build a heap from an unordered sequence.

2. After the top element of the output heap, adjust the remaining element to become a new heap.


The following uses the sequence {49, 38, 65, 97, 76, 13, 27, 49} as an example to analyze how to create an initial heap and how to adjust the new heap after outputting the heap top element.


The process of building a heap from an unordered sequence is a process of repeated "screening. If we regard this sequence as a Complete Binary Tree, the last non-terminal node is the [n/2] element, therefore, "filter" starts with the [n/2] element. Take the construction of the "minimum Heap" as an example:


49

/\ Figure a: Since n/2 is rounded down to 4, the value of the 4th elements is 97,

38 65 the non-terminal node value in the minimum heap should be smaller

The value of the left and right children. Here 97> 49, So 97 and 49 are exchanged.

97 76 13 27.

/

49

()


49

/\ Figure B: Filter 3rd elements in Order 65,

38 65 as 65> 13, switch 65 to 13 to get Figure c.

/\/\

49 76 13 27

/

97

(B)


49

/\ Figure c: Filter 2nd elements by Order 38,

38 13 because 38 is not greater than the left and right children, the screening sequence remains unchanged.

/\/\

49 76 65 27

/

97

(C)


49

/\ Figure d: Filter 1st elements by Order 49,

38 13 because 49> 13,

// \/\ And because the element 49 after the switch is still greater than 27,

49 76 65 27 continue to exchange with 27. The constructed heap is shown in Figure e.

/

97

(D)


13

/\ Figure e: constructed heap.

38 27

/\/\

49 76 65 49

/

97

(E)



After creating a new heap, you can select the elements at the top of the heap as the minimum value, and then adjust the remaining elements after the elements at the top of the output heap to become a new heap. To retrieve the small value. The heap constructed above is used as an example:


13

/\ Figure a: After the constructed heap top element 13 is sorted

38 27 the first element of the ordered sequence: {13 }.

/\// Output the top element 13 of the heap and replace it with the last element 97 in the heap.

49 76 65 49. Figure B is displayed.

/

97

()


97

/\ Figure B: Place 97 behind the root node, and its left and right subtree (marked with green and red in the figure)

38 27 is still heap. You only need to adjust from top to bottom so that the entire number is heap. First, use the heap

/\// The value of the top Element and Its left and right child root nodes is compared, because the value of the right child Root Node

49 76 65 49 if the value of the left child root node is smaller than the value of the root node, 27 and 97 are exchanged.

Figure c is displayed.

13

(B)


27

/\ Figure c: Because 97 replaces 27 and then destroys the "Heap" of the right subtree, you need

38 97 the same adjustment until the leaf node is reached, and Figure d is displayed.

/\/\

49 76 65 49

13

(C)


27

/\ Figure d: At this time, the binary tree is a heap, and the top of the heap is the minimum value among n-1 elements. Retrieve

38 49 the minimum value of the n-1 element (the minimum value of the n element ).

/\/\ Repeat the previous steps to obtain the smallest values in sequence. To complete the heap sorting.

49 76 65 97

13 27

(D)


Note: The heap sorting method is not recommended for files with a small number of records, but it is very effective for files with a large number of records. In the worst case, the time complexity of heap sorting is O (nlogn ). Compared with quick sorting, this is the biggest advantage of heap sorting. In addition, the heap sorting only requires one record size for the secondary storage space for interchange.


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.