Java implementation heap sequencing (Dagen)

Source: Internet
Author: User

Heap sorting is a kind of tree-selecting sorting method, which is characterized by: in the process of sorting, the array[0,...,n-1] is considered as a sequential storage structure of a complete binary tree , using the intrinsic relationship between the parent node and the child node in the complete binary tree, Select the maximum (minimum) element of the keyword in the current unordered region.

1. If array[0,...,n-1] represents a sequential storage pattern for a fully binary tree, the intrinsic relationship between the parent node pointer and the child node pointer is as follows:

Any node pointer I: parent node: i==0? Null: (I-1)/2

Left Child: 2*i + 1

Right Child: 2*i + 2

2. Heap definition: n keyword sequence array[0,...,n-1], when and only if the following requirements are met: (0 <= I <= (n-1)/2)

①array[i] <= Array[2*i + 1] and Array[i] <= array[2*i + 2]; called small Gan;

②array[i] >= Array[2*i + 1] and Array[i] >= array[2*i + 2]; called Dagen;

3. Establish Dagen:

n nodes of the complete binary tree array[0,...,n-1], the last node n-1 is the child (n-1-1)/2 nodes. The subtree is adjusted to the root of the (n-1-1)/2 nodes, so that the subtree is called a heap.

For Dagen, the adjustment method is: If the "root node keyword" is less than "the key word in the left and right children", then the exchange.

Then forward to each node ((n-2)/2-1) ~ 0 as the root of the subtree to adjust to see if the node value is greater than the value of its left and right child nodes, if not, the left and right child nodes in the exchange of large values, the exchange may break the next heap, and then continue to use the above method to build the next heap, Until the heap is made up of a subtree that is the root of the node.

Reuse the above-mentioned adjustment heap method to build the heap until the root node.

4. Heap sequencing: (Dagen)

① The n elements stored in array[0,...,n-1] into the initial heap;

② the top element of the heap is exchanged with the base element, the maximum value of the sequence is placed in the correct position;

③ But at this point the heap is destroyed, the top element of the heap is adjusted downward so that it continues to maintain the nature of Dagen, and then the ②③ step is repeated until only one element is left in the heap.

Performance analysis of heap sorting algorithm:

Space complexity: O (1);

Time Complexity: Build heap: O (n), each adjustment O (log n), so the best, worst, average case: O (N*LOGN);

Stability: Unstable

Methods for establishing Dagen:

1     //build Dagen: Treat array as a sequential storage structure of a complete binary tree2     Private int[] Buildmaxheap (int[] Array) {3         //start with the parent node of the last node array.length-1 (array.length-1-1)/2, and adjust the heap repeatedly until the root node 04          for(intI= (array.length-2)/2;i>=0;i--){ 5 adjustdowntoup (array, i,array.length);6         }7         returnArray;8     }9     Ten     //gradually adjust the tree structure from bottom to top of the element Array[k] One     Private voidAdjustdowntoup (int[] Array,intKintlength) { A         inttemp =Array[k];  -          for(inti=2*k+1; i<length-1; i=2*i+1) {//I is the left child initialized to Node K, which is adjusted downward along the larger child nodes -             if(I<length && array[i]<array[i+1]) {//take the subscript of a node with a large sub-node thei++;//if the right child of the node > left child, take the right child node subscript -             } -             if(Temp>=array[i]) {//root node >= The key words in the children, adjust the end -                  Break; +}Else{//root node < left and right children with the larger keywords -ARRAY[K] = Array[i];//Adjust the large value array[i] on the parent node to the left and right child nodes . +K = i;//"Critical" modifies the K value to continue the downward adjustment A             } at         } -ARRAY[K] = temp;//The value of the adjusted node puts the final position of the person -}

Heap Sort:

1     //Heap Sort2      Public int[] Heapsort (int[] Array) {3Array = buildmaxheap (array);//Initial build heap, array[0] is the element with the largest first trip value4          for(inti=array.length-1;i>1;i--){  5             inttemp = array[0];//swap the top element of the heap with the low element of the heap to get the correct sort position for the current largest element6Array[0] =Array[i];7Array[i] =temp;8Adjustdowntoup (array, 0,i);//organize the remaining elements into piles9         }Ten         returnArray; One}

Delete the top element of the heap (that is, the maximum in the sequence): first, the last element of the heap is exchanged with the top element of the heap, because the nature of the heap is destroyed and the root node at this point is adjusted downward.

1     //Delete Heap top element action2      Public int[] Deletemax (int[] Array) {3         //swaps the last element of the heap with the top element of the heap, with the base element value set to -999994Array[0] = array[array.length-1];5ARRAY[ARRAY.LENGTH-1] = 99999;6         //downward adjustment to the root node at this point7Adjustdowntoup (Array, 0, array.length);8         returnArray;9}

Insert operations on the heap: first place the new node at the end of the heap, and then perform an upward adjustment on the new node.

Assuming that the last element of the array array[array.length-1] is empty, the newly inserted node is initially placed here.

1     //Insert operation: Inserts data into the Dagen array2      Public int[] InsertData (int[] Array,intdata) {3ARRAY[ARRAY.LENGTH-1] = data;//Place the new node at the end of the heap4         intK = array.length-1;//nodes that need to be adjusted5         intParent = (k-1)/2;//parent Node6          while(Parent >=0 && data>Array[parent]) {7ARRAY[K] = array[parent];//Parental node Reduction8K =parent;9             if(Parent! = 0){TenParent = (parent-1)/2;//continue to compare upward One}Else{//The root node has been adjusted to jump out of the loop A                  Break; -             } -         } theARRAY[K] = data;//put the inserted node in the correct position -         returnArray; -}

Test:

1      Public voidToString (int[] Array) {2          for(intI:array) {3System.out.print (i+ "");4         }5     }6     7      Public Static voidMain (String args[]) {8Heapsort HS =Newheapsort ();9         int[] Array = {87,45,78,32,17,65,53,9,122};TenSystem.out.print ("Build Dagen:"); One hs.tostring (hs.buildmaxheap (array)); ASystem.out.print ("\ n" + "delete heap top element:"); - hs.tostring (Hs.deletemax (array)); -System.out.print ("\ n" + "Insert element 63:"); theHs.tostring (Hs.insertdata (array, 63)); -System.out.print ("\ n" + "Big root heap Sort:"); - hs.tostring (Hs.heapsort (array));  -}
1 2 3 4

Java implementation heap sequencing (Dagen)

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.