Basic Algorithms-heap sorting and Java Implementation

Source: Internet
Author: User

The heap data structure is an array object, which can be regarded as a complete binary tree. You can retrieve the corresponding array by traversing the binary tree hierarchy from the root of the binary tree. The root node of the tree is A [0]. For the coordinate I of A node in the tree, the coordinates of the left and right child nodes and the Father's Day node can be easily obtained:

LEFT (I) = 2 * I + 1; RIGHT (I) = 2 * I + 2; PARENT (I) = I/2.

There are two types of binary heap: Maximum heap and minimum heap. In the largest heap, the storage value of each node is greater than or equal to the storage value of the left and right child nodes, that is, A [I]> = A [LEFT [I] & A [I]> = A [RIGHT [I]. The smallest heap is the opposite. This article takes the largest heap as an example.

After knowing the definition of the maximum heap, we need to build the maximum heap on any given array, and then sort the largest heap in ascending order:

(1) Build the largest heap:

First, we define a method Heapify (heap, I). If the subtree with the left and right child nodes of I as the root is already the largest heap, this method also makes the subtree with I as the root become the largest heap. Its pseudocode is as follows (from introduction to algorithms):

1 MAX-HEAPIFY (A, I) 2 l = LEFT (I) 3 r = RIGHT (I) 4 target = I; 5 if (l <=. heap_size & A [l]> A [I]) 6 target = l; 7 if (r <=. heap_size & A [r]> A [I]) 8 target = r; 9 if (target! = I) 10 exchange (A [I], A [target]) 11 MAX-HEAPIFY (A, target) // Recursion

We compare the value of the current node I with the value of its left and right subnodes. If the value of a subnode of I is greater than the value of I, it violates the definition of the maximum heap, therefore, we need to switch the locations of these two nodes. Assume that A [LEFT [I]> A [RIGHT [I]> A [I], the I and LEFT [I] nodes are exchanged. However, the I node switched to the left child node may violate the definition of the largest heap with the left child node as the root, so we need to call the MAX-HEAPIFY method recursively on this largest heap.

With the above method, we can call the MAX-HEAPIFY method from the bottom up to build the largest heap. Because A [. length/2 + 1] and later nodes are leaf nodes, which can be regarded as the maximum heap with only one node, so we can start from A [. length/2] nodes start until A [0] node calls the MAX-HEAPIFY method in turn, that is, from the tree level traversal of the last node with children, the MAX-HEAPIFY method is called in reverse order based on layered traversal:

1 BUILD-MAX-HEAP(A)2  for i=A.length/2 downto 13     MAX-HEAPIFY(A,i)

(2) Heap sorting

After constructing the maximum heap, we can use it for sorting. Because the max heap can only ensure that A [0] stores the largest element in the current heap, we can swap A [0] with the last element of the heap, in this way, A [0] is in the last and correct position. At this time, the last position is no longer the largest heap, so A. heap_size should be reduced by one. Element swapping to A [0] may damage the nature of the Max heap, so we can call the MAX-HEAPIFY method to make it the largest heap again, then swap A [0] to the last position of the current heap. Recursion.

1 HEAPSORT (A) 2 for I =. length downto 23 exchange (A [I], A [0]) 4 --. heap_size // reduce heap size by 5 MAX-HEAPIFY (A, 0)

(3) JAVA Implementation of heap

 

1 package Algorithm; 2 3 import java. util. *; 4/** 5 * Heap (Heap) 6 * @ author Kemaswill 7*2012-10-5 Friday 8 */9 10 public class Heap {11 12 public static int [] data; 13 public static int length; 14 public static int heap_size; 15 16 public Heap () {17 this. length = 20; 18 this. heap_size = length; 19 this. data = new int [length]; 20 Random random = new Random (System. currentTimeMillis (); 21 for (int I = 0; I <length; I ++) {22 data [I] = Math. abs (random. nextInt () % 50); 23} // for24} 25 26 public Heap (int n) {27 this. length = n; 28 this. heap_size = length; 29 this. data = new int [length]; 30 Random random = new Random (System. currentTimeMillis (); 31 for (int I = 0; I <length; I ++) {32 data [I] = Math. abs (random. nextInt () % 50); 33} // for34} 35 36 public static void max_heapify (Heap heap, int I) {37 if (I 

 

References:

[1] Chapter 6th of Introduction to algorithms p73

[2] A simple Max heap implemented in Java

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.