Heap sort Maximum heap minimum heap Java implementation

Source: Internet
Author: User

Heap sort Ah, actually is a data structure, binary tree, binary tree is divided into two fork tree and complete binary tree. A depth of k, and there are 2k-1 nodes called full two fork tree, complete binary tree: The depth of k, there are n nodes of the two-tree, when and only if each of its nodes with a depth of k in the full two-tree with a number of 1 to n nodes corresponding to the node, called a complete binary tree.
Words retract, heap, have the largest heap, the smallest heap, the largest heap is the child node is no smaller than the parent node, the smallest heap is the opposite. Here is the heap sort, you can draw a sketch of your own painting, heap sorting process. 、

Solve the problem:
The TOPK problem is the maximum (or minimum) k data obtained from a large amount of data (source data).

Package Com.lhcis.bond.business.constant;public class Heapsortfinal {public static void main (string[] args) {i        Nt[] Array = {19, 38, 7, 36, 5, 5, 3, 2, 1, 0, 56};        System.out.println ("Before sorting:");        for (int i = 0; i < Array.Length; i++) {System.out.print (Array[i] + ",");        } System.out.println ();        SYSTEM.OUT.PRINTLN ("Split Line---------------");        Heapsort (array);        System.out.println ("After sorting:");        for (int i = 0; i < Array.Length; i++) {System.out.print (Array[i] + ",");        }} public static void Heapsort (int[] array) {if (array = = NULL | | array.length = = 1) return; Buildmaxheap (array); First order, build the largest heap, only ensure that the heap top element is the largest in the array for (int i = array.length-1; I >= 1; i--) {//What does this mean?            , after some of the above column operations, currently ARRAY[0] is the largest element in the current array, need and the end of the element Exchange//Then, take out the largest element swap (array, 0, i); After the exchange, the next time you traverse, you should skip the last element, that is, the largest value, and then start to rebuild the maximum heap//heap size minus 1, and then fromThe position of 0 starts with the maximum heap//maxheap (array, I, 0);        Minheap (array, I, 0);            }}//Build heap public static void Buildmaxheap (int[] array) {if (array = = NULL | | array.length = = 1)        Return        The formula for the heap is int root = 2*i, int left = 2*i+1, and int right = 2*i+2;        int cursor = ARRAY.LENGTH/2;            for (int i = cursor; I >= 0; i--) {//This allows for the first sort completion//maxheap (array, array.length, I) under the For loop;        Minheap (array, array.length, i); }}//Max heap public static void Maxheap (int[] array, int. heapsieze, int index) {int left = index * 2 + 1; /Left Dial hand node int right = Index * 2 + 2; Right child node int maxValue = index; The position of the index at the moment is the maximum//if the value of the left Dial hand node is larger than the current maximum value, the position of the maximum is changed to the position of the left child node if (Leave < Heapsieze && Array[left] & Gt        Array[maxvalue]) {maxValue = left; }//If the value of the right child node is larger than the current maximum value, change the position of the maximum value to the position of the right child node if (R < Heapsieze && Array[right] > Array[maxvalu E]) {           MaxValue = right;  }//If not equal, it means that the value of this child node is larger than itself, position has swapped position if (maxValue! = index) {Swap (array, index, maxValue); It is necessary to exchange the position element//To determine whether the sub-node breaks the property of the maximum heap after exchanging the position.            Maximum heap properties: Two child nodes are smaller than the parent node.        Maxheap (Array, Heapsieze, maxValue); }}//min heap public static void Minheap (int[] array, int heapsieze, int. index) {int left = index * 2 + 1; /Left Dial hand node int right = Index * 2 + 2; Right child node int maxValue = index; The position of the index at the moment is the minimum value//If the value of the left Dial hand node is smaller than the current minimum value, the position of the minimum value is changed to the position of the left child node if (Leave < Heapsieze && Array[left] & Lt        Array[maxvalue]) {maxValue = left; }//If the value of the right child node is smaller than the current minimum value, change the position of the minimum to the position of the left child node if (R < Heapsieze && Array[right] < Array[maxval        UE]) {maxValue = right;  }//If not equal, the value of this child node is smaller than itself, position has swapped position if (maxValue! = index) {Swap (array, index, maxValue); It is necessary to exchange position elements//To determine whether the sub-nodes break the minimum heap properties after exchanging the position. MostSmall properties: Two child nodes are larger than the parent node.        Minheap (Array, Heapsieze, maxValue);        }}//array element interchange public static void swap (int[] array, int index1, int index2) {int temp = array[index1];        ARRAY[INDEX1] = Array[index2];    ARRAY[INDEX2] = temp; }}

Heap Sort Maximum heap minimum heap Java implementation

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.