1. What is an array object in the heap (Binary) Heap Data Structure? It can be regarded as a Complete Binary Tree. Each node in the tree corresponds to the element that stores the node in the array. Each layer of the tree is filled, except for the last layer.
A piece is the correspondence between a heap structure (left) and an array (right). The storage format is still an array, but it can be viewed as a heap structure. Note that the graph starts from 1, while the array in Java starts from 0. Calculation Method of array indexes for Left and Right subnodes of any node I: 2i for left subnode and 2i + 1 for right subnode. For example, if I = 4, the index value of the Left subnode is 2 * I = 2*4 = 8, the index value of the right subnode is 2 * I + 1 = 2*4 + 1 = 9.
However, the array in Java starts from 0. Therefore, in Java, the calculation method of the array index of the left and right subnodes of I should be: index the left subnode in the array: 2i + 1 with subnodes indexed in the array: 2i + 2
2. Keep the heap in two ways: the maximum heap and the minimum heap. Both of them must satisfy the characteristics of the heap, and if the maximum heap must ensure that the parent node in each layer must be greater than its left and right subnodes, the minimum heap rule is the opposite. First, let's take a look at any element in the array to meet the above conditions of the maximum heap.
(1) pseudo code that allows any element in the array to meet the maximum heap rule:
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 ← l5 else largest ← i6 if r ≤ heap-size[A] and A[r] > A[largest]7 then largest ← r8 if largest ≠ i9 then exchange A[i] <-> A[largest]10 MAX-HEAPIFY(A, largest)
(2) The above pseudo code is explained one by one. calculate the left subnode index 2. calculate the index of the right subnode 3. if the left subnode array index is smaller than or equal to the array size and the left subnode is greater than the parent node 4. assign the array index of the Left subnode to largest 5. if the three conditions are not met, assign the array index of the current parent node to largest6. if the array index of the right child node is smaller than or equal to the array size, and the array index of the right child node is greater than largest 7. if the 6 conditions are met, the right subnode is assigned to largest8. if you need to switch 9. exchange the current parent node with largest (maximum number of child nodes) 10. use the largest node as the parent node for the next execution, and then start from 1.
(3) Java code implementation of pseudo code
Private void maxHeapify (int [] a, int I) {int largest; int leftIndex = 2 * I + 1; int rightIndex = leftIndex + 1; if (leftIndex <. length & a [leftIndex]> a [I]) {largest = leftIndex;} else {largest = I;} if (rightIndex <. length & a [rightIndex]> a [largest]) {largest = rightIndex;} if (largest! = I) {swap (a, I, largest); maxHeapify (a, largest) ;}} public static void main (String [] args) {int [] array = {16, 4, 10, 14, 7, 9, 3, 2, 8, 1}; HeapSort heapSort = new HeapSort (); // Java array starts from 0, and the second element index is 1heapSort. maxHeapify (array, 1); System. out. println (Arrays. toString (array ));}
Input array: [16, 4, 10, 14, 7, 9, 3, 2, 8, 1] output result: [16, 14, 10, 8, 7, 9, 3, 2, 4, 1] (4) demonstrate the execution process when I = 2
The heap processing is performed on the I = 2 parent node in the heap, that is, to ensure that the values of the left and right child nodes of the parent node I are smaller than their own. The following are the steps to describe the image in text: 1. first look at the (a) graph. When I = 2, array [I] = array [2] = 4 left subnode array [leftIndex] = array [4] = 14, the judgment condition is array [leftIndex]> array [I] = true. The left subnode is greater than the parent node value and does not meet the maximum heap condition. Right subnode array [rightIndex] = array [5] = 7, judgment condition array [rightIndex]> (maximum value between parent node and left subnode, currently array [leftIndex]) = false: The right child node is smaller than the left child node. Therefore, the left child node as the parent node meets the maximum heap condition. Exchange the values of I = 2 and leftIndex = 4, that is, the result displayed by (B ).
2. in the (B) diagram, step 1 determines that the I = 2 node has met the maximum heap condition, but you need to judge again whether the I = 4 value after the switch meets the maximum heap condition. When I = 4, array [I] = array [4] = 4, left subnode array [leftIndex] = array [8] = 2, the judgment condition is array [leftIndex]> array [I] = false. The left subnode meets the maximum heap condition. Right subnode array [rightIndex] = array [9] = 8, judgment condition array [rightIndex]> (maximum value between parent node and left subnode, currently array [I]) = true: the value of the right child node is greater than that of the parent node and does not meet the maximum heap condition. Exchange the values of I = 4 and rightIndex = 9, that is, the result displayed in (c ).
3. Look at the (c) diagram. Because I = 9 does not have any left or right subnodes, this processing is complete.
3. Create a maximum heap. 2. Maintain the heap nature. The specific operation is to ensure that the processed node and all its subnodes meet the conditions of the maximum heap, however, each operation can only ensure that the specified node meets the conditions. If you need a complete maximum heap, you must perform the above operations on all nodes. However, to reduce the number of executions, do not perform the above operations on all nodes at the bottom layer in the heap, because there are no left or right subnodes (for example, nodes 8, 9, and 10) at the bottom layer of the node, the length/2-1 is sorted by array, the index of the last parent node (for example, Medium five nodes) is then decreased in sequence (for example, Medium five, four, three, two, and one ).
(1) Create the maximum heap pseudo code
BUILD-MAX-HEAP(A)1 heap-size[A] ← length[A]2 for i ← |_length[A]/2_| downto 13 do MAX-HEAPIFY(A, i)
(2) Create the max heap Java Implementation and example processing result
public void buildMaxHeap(int[] a, int n) { for (int i = n/2-1 ; i >= 0; i--) { maxHeapify(a, i, n); } } public static void main(String[] args) { int[] array = {4, 1, 3, 2, 16, 9, 10, 14, 8, 7}; System.out.println( Arrays.toString(array) ); HeapSort heapSort = new HeapSort(); heapSort.buildMaxHeap(array, array.length); System.out.println( Arrays.toString(array) ); }
Input array: [4, 1, 3, 2, 16, 9, 10, 14, 8, 7] output result: [16, 14, 10, 8, 7, 9, 3, 2, 4, 1]
3. Maximum heap creation process
The above is the nature of the maximum heap for the I = 5, 4, 3, 2, and 1 nodes, which is not explained here, you can view the maximum heap of each node (2. Maintain the nature of the maximum heap-> (4) demonstrate the execution process when I = 2)
4. What is heap as explained above? If every node in the heap enjoys the nature of the largest heap, the largest heap is also created. The above three steps are all prepared for heap sorting. Is there a question about whether the largest heap is a sorted array? To solve this problem, you can view the results of the three largest heap types and view the results ([16, 14, 10, 8, 7, 9, 3, 2, 4, 1]) whether or not the sorting requirements are met. A strong optimistic result is that the sorting from big to small is satisfied, but 7 and 2 are obviously smaller than the value on the right, therefore, the largest heap is not necessarily a sorted array. The maximum Heap has a feature. Its root node is the largest in the heap. If you can remove the root node from the heap, then keep the remaining nodes as the maximum heap feature, the maximum heap operation to remove and retain the remaining nodes repeatedly. Because each operation removes the largest value from the remaining node, an array may be created.
(1) Heap sorting pseudo code where the BUILD-MAX-HEAP has been introduced in (2. Keep heap properties) and the MAX-HEAPIFY has been introduced in (3. Create Max heap)
HEAPSORT(A)1 BUILD-MAX-HEAP(A)2 for i ← length[A] downto 23 do exchange A[1] <-> A[i]4 heap-size[A] ← heap-size[A] - 15 MAX-HEAPIFY(A, 1)
(2) Complete heap sorting code
public class HeapSort { public static void main(String[] args) { int[] array = {4, 1, 3, 2, 16, 9, 10, 14, 8, 7}; System.out.println( Arrays.toString(array) ); HeapSort heapSort = new HeapSort(); heapSort.heapSort(array, array.length); System.out.println( Arrays.toString(array) ); } public void heapSort(int[] array, int n) { if (array == null) { throw new NullPointerException(); } if (n > array.length) { throw new ArrayIndexOutOfBoundsException(); } buildMaxHeap(array, n); for (int i = n-1; i >= 1; i--) { swap(array, 0, i); maxHeapify(array, 0, i); } } public void buildMaxHeap(int[] a, int n) { for (int i = n/2-1 ; i >= 0; i--) { maxHeapify(a, i, n); } } private void maxHeapify(int[] a, int i, int n) { int largest; int leftIndex = 2 * i + 1; int rightIndex = leftIndex + 1; if (leftIndex < n && a[leftIndex] > a[i]) { largest = leftIndex; } else { largest = i; } if (rightIndex < n && a[rightIndex] > a[largest]) { largest = rightIndex; } if (largest != i) { swap(a, i, largest); maxHeapify(a, largest, n); } } private void swap(int[] pArray, int pX, int pY) { int temp = pArray[pX]; pArray[pX] = pArray[pY]; pArray[pY] = temp; }}
Note: The maxHeapify method differs from the preceding method by adding the total number of n heap elements, because the heap size is equal to 1 after each root removal, therefore, the parameter that inputs the total number of heap elements each time the maximum heap is created is the value after the root is removed.
Input array: [4, 1, 3, 2, 16, 9, 10, 14, 8, 7] output result: [1, 2, 3, 4, 7, 8, 9, 10, 14, 16]
V. References Chapter 6 heap sorting
Master eight sorting algorithm series: 2. Heap sorting algorithm the images in this article are from this article. The original source is introduction to algorithms.
Seven heap and heap sorting in the Classical Vernacular algorithm series
Classic sorting algorithm-Heap sorting sort
Back to Java Learning Article-Index