Java Implementation heap Sequencing

Source: Internet
Author: User

Heap

Heap sort and merge sort, is a time complexity O (NLGN) algorithm, as well as the insertion sort, is an in-place sorting algorithm (no need for additional storage space). Heap sequencing requires a data structure called the largest heap, unlike a Java or Lisp GC, where the heap is a data structure that can be seen as a complete binary tree, in which the other layers in the tree are filled except for the last layer. Because of this, the number of children and parents of each node in the tree can be directly calculated according to the number of the current node.

Parent (i) =I/2

Left (i) =2*i

Right (i) =2*i+1

As shown, the 1-Position children node is 2, 3 2 nodes of the child node is 4, 5 2 of the parent node is 1 to investigate the other nodes are also easy to find the above relationship. The maximum heap is a special heap, characterized by the value of each parent node being larger than the child node. His characteristic allows him to achieve NLGN in-situ sequencing. Now let's look at how to build and maintain a maximum heap.

Construction and retention of the maximum heap

We now have an array a, the size is n, assuming that the elements are arranged in a completely binary tree manner. How do you construct it as a maximum heap? First we know that each subtree of the largest heap conforms to the nature of the maximum heap (the root node value is greater than all child nodes). At the same time we know that the sequence number is (n/2+1) ~n element is the leaf node (because its children node number is greater than n, that is, there is no child node), so we build the largest heap operation is in the element ordinal is 1~N/2 (the other elements have satisfied the maximum heap nature). We define the following operation Maxify (i): Transform the subtree with the I position node root into the largest heap. Its operation is as follows: For each node I, we examine the size of his and children's nodes, if he is smaller than a child node, then he and the child node of the largest swap position, and then in the corresponding child node position repeated operations until the leaf node to reach the heap or the location of the child node is larger than the value of the. The process of constructing the maximum heap buildmaxheap is to call the maxify process on each internal node, then to the root of the tree, at which point the left and right subtree are the largest heap, and now the maximum heap is completed by calling Maxify on the root node.

Operations for Heap sorting

With the largest heap of infrastructure, we can sort heapsort with the nature of the largest heap, we start with the root node, because the root node is the largest element in the array, so we'll swap it in the last element in the array (after sorting, the largest element should be at the end) minus the heapsize by 1. , and then call the Maxify procedure at the root node to re-heap the new heap to the maximum. In turn, we can place the largest element of an existing heap at the end of the heap each time. Finally, the entire sequencing process is completed. See the operating conditions (only the first 4 steps listed)

 PackageCom.ys.sort;Importjava.util.Arrays;/** Make use of "big talk data Structure" 9th up heap sort to modify * Note that in Java array starting from 0, the node s corresponding to the child is 2s,2s+1; * node S in Java is represented as A[s-1], left and right children should be a[2s],a[2s-1]
* no use of recursion **/ Public classHeapsort { Public voidHeapsort (int[] Array) {maxheapadjust (array); //built into a large top cone for(inti=array.length-1;i>0;i--) {Swap (array,0, i); Heapadjust (Array,1,i);//re-adjust to large top heap } } Public voidMaxheapadjust (int[] Array) { for(inti=array.length/2;i>0;i--) {//built into a large top coneHeapadjust (array,i,array.length); } } Public voidHeapadjust (int[] A,intSintm) { intTemp,i,largest;//largest record subscript with large keywordstemp = a[s-1];//represents the first S node for(i=2*s;i<=m;i*=2){ if(I<m && a[i-1]<A[i]) {Largest=i; ++i; }Elselargest= I-1; if(temp>=A[largest]) Break; A[s-1] =A[largest]; S= Largest+1; } a[s-1] =temp; } Public voidSwapint[] Array,intFromintTo ) { inttemp; Temp=Array[from]; Array[from]=Array[to]; Array[to]=temp; } Public Static voidMain (string[] args) {heapsort h=NewHeapsort (); int[] a= {16, 14, 10, 8, 7, 9, 3, 2, 4, 1}; H.heapsort (a); String intarraystring= Arrays.tostring (a);//convert an array to a string print outputSystem.out.print (intarraystring); }}

Theoretical part transferred from http://www.cnblogs.com/developerY/p/3319618.html

Java Implementation heap Sequencing

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.