3. Compare and sort the heap and heap

Source: Internet
Author: User

3. Compare and sort the heap and heap

 

Full Binary Tree Knowledge is involved in heap sorting. For the {10, 2, 11, 8, 7} columns to be sorted, it is regarded as a Complete Binary Tree, as shown in.

A heap is divided into a large root heap and a small root heap. A large root heap indicates that each root node is greater than its subnode (L (I)> = L (2i) & L (I)> = L (2i + 1). A small root heap indicates that each root node is smaller than its subnode (L (I) <= L (2i) & L (I) <= L (2i + 1 )). (In a Complete Binary Tree, the left subnode of node I is 2i, And the right byte is 2i + 1)

This article will explain how to build a large-root heap as an example.

The first step of heap sorting is to build the initial heap. How to Build the initial heap? According to the definition, the key point lies in each root node. Observe the Complete Binary Tree of the columns to be sorted. It is not difficult to find that node 2 and node 10 have subnodes, which are the nodes to be concerned about.

How to locate Node 2? It is found that it is a leaf node or the parent node of the last node. According to the nature of the full binary tree, the number of the parent node of any node except the root node is limit n/2 bytes ⌋. If n = 5 is known, the number of Node 2 is unknown 5/2 bytes = ②. Compare it with the size of the left and right subnodes and adjust it.

The root node 10 is left. The number of the known Node 2 is ②, and the number of the root node 10 is obtained after ②-1 = ①. Compare it with the size of the left and right subnodes and adjust it.

After the adjustment, it is found that a "big root Heap" has been formed. In the example, the columns to be sorted are relatively simple, and a more complex column to be sorted is given to observe the process of building a large root heap. For the {53, 17, 78, 09, 45, 65, 87, 32} columns to be sorted, it is regarded as a Complete Binary Tree.

Let's also look at the nodes that need attention.

Based on the first example, we can easily locate node 09 with serial 8/2 Limit = ④ and node 78 with serial numbers ④-1 = ③ ......, And so on.The node location to be adjusted fromBytes ⌊N/2Bytes ⌋Start to decrease sequentially until the end of root node ① (Bytes ⌊N/2Bytes ⌋~ 1). Now start to adjust.

After the fourth adjustment, it is found that node 53 does not meet the definition of the big root heap, and its right child node is greater than it. further downward adjustment is required.

 

Note that the downward adjustment is to determine whether downward adjustment is required for each upward adjustment, rather than going back and adjusting it after all the upward adjustments are completed. In this way, the large root heap is established. The array of the columns to be sorted has changed: {87, 45, 78, 32, 17, 65, 53, 09 }. The next question is how to sort data. Swap the root node of the big root heap with the last node, and adjust the binary tree so that it still meets the requirements of the big root heap.

You can see that after calling the root node and the last node, the maximum value of the column to be sorted has been placed at the last position of the array {......, 87}. At this time, the first sorting is completed, but the first sorting is not over yet. In addition to node 87, other nodes do not meet the conditions of the big root heap, therefore, you need to adjust the remaining nodes to a large heap. The sorting process is no longer given. The Java and Python3 code implementation is as follows.

Java

1 package com. algorithm. sort. heap; 2 3 import java. util. arrays; 4 5/** 6 * heap sorting 7 * Created by yulinfeng on 6/20/17. 8 */9 public class Heap {10 11 public static void main (String [] args) {12 int [] nums = {53, 17, 78, 09, 45, 65, 87, 32}; 13 nums = heapSort (nums); 14 System. out. println (Arrays. toString (nums )); 15} 16 17/** 18 * heap sorting 19 * @ param nums to be sorted array sequence 20 * @ return sort the ordered array sequence 21 */22 private static int [] heapSort (int [] nums) {23 24 for (int I = nums. length/2-1; I> = 0; I --) {25 heapAdjust (nums, I, nums. length); 26} 27 for (int I = nums. length-1; I> 0; I --) {28 int temp = nums [I]; 29 nums [I] = nums [0]; 30 nums [0] = temp; 31 heapAdjust (nums, 0, I); 32} 33 return nums; 34} 35 36/** 37 * adjust heap 38*39 * @ param nums waiting for sorting sequence 40 * @ param parent waiting for adjusting root node 41 * @ param length array sequence length 42*/43 private static void heapAdjust (int [] nums, int parent, int length) {44 int temp = nums [parent]; 45 int childIndex = 2 * parent + 1; // The Position of the Left subnode of the full Binary Tree node I starting from 1 is 2i, where the array subscript starts from 0, that is, the index position of the array where the left subnode is located is: 2i + 146 while (childIndex <length) {47 if (childIndex + 1 <length & nums [childIndex] <nums [childIndex + 1]) {48 childIndex ++; // if the node has a right subnode and the right subnode is greater than the left subnode, select the right subnode 49} 50 if (temp> nums [childIndex]) {51 break; // If the selected node is greater than its subnode, return 52} 53 nums [parent] = nums [childIndex]; 54 parent = childIndex; 55 childIndex = 2 * parent + 1; // continue to adjust down 56} 57 nums [parent] = temp; 58} 59}

Python3

1 # heap sorting 2 def heap_sort (nums): 3 4 for I in range (int (len (nums)/2-1),-1,-1 ): 5 heap_adjust (nums, I, len (nums) 6 7 for I in range (len (nums)-1,-1,-1 ): 8 temp = nums [I] 9 nums [I] = nums [0] 10 nums [0] = temp11 heap_adjust (nums, 0, I) 12 13 return nums14 15 # adjust heap 16 def heap_adjust (nums, parent, length): 17 18 temp = nums [parent] 19 childIndex = 2 * parent + 120 while childIndex <length: 21 if childIndex + 1 <length and nums [childIndex] <nums [childIndex + 1]: 22 childIndex + = 123 if temp> nums [childIndex]: 24 break25 nums [parent] = nums [childIndex] 26 parent = childIndex27 childIndex = 2 * parent + 128 29 nums [parent] = temp30 31 nums = [53, 17, 78, 09, 45, 65, 87, 32]
32 nums = heap_sort (nums) 33 print (nums)

 

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.