First, Introduction
Heap sorting (heapsort) is an improved version of the choice of sorting, it can reduce the number of comparisons in the selection of sorting, and thus reduce the sorting time, heap sorting method used to two fork tree technique, it uses the accumulation of trees to complete, accumulation is a special two fork tree, can be divided into Dagen and small Gan.
Dagen Required Conditions:
It's a completely binary tree.
The value of all nodes is greater than or equal to the value of its left and right child nodes
The root is the largest of the stacked numbers.
Requirements for small Gan:
It's a completely binary tree.
The value of all nodes is less than or equal to the value of its left and right child nodes
The root is the smallest of the stacked numbers.
Second, the core idea
Using the Dagen (Keng Gen) heap top record is the maximum keyword (minimum keyword) that makes it easy to select the maximum record (minimum record) each time from the unordered order.
The basic idea is (Dagen):
1) The initial order to sort the keyword sequence (r1,r2 .... Rn) is constructed into a large root heap, which is the initial no-need zone;
2) Swap the top element of the heap r[1] with the last element R[n] to get a new unordered area (R1,R2,...... RN-1) and the new ordered area (Rn), and satisfies the r[1,2...n-1]<=r[n];
3) due to the new heap top r[1] may violate the nature of the heap, it is necessary to the current unordered zone (R1,R2,...... RN-1) adjusts to the new heap, then swaps the r[1] with the last element of the unordered zone, resulting in a new unordered area (R1,R2 ...). Rn-2) and the new ordered area (RN-1,RN). This process is repeated until the number of elements in the ordered area is n-1, and the entire sorting process is complete.
The operation process is as follows:
1) Initialize the heap: R[1..N] is constructed as a heap;
2) Swap the top element of the current unordered area R[1] with the last record of the interval, and then adjust the new unordered area to the new heap.
So for heap sequencing, the most important two operations are to construct the initial heap and adjust the heap, in fact, the construction of the initial heap is actually the process of adjusting the heap, but the initial heap is to construct all the non-leaf nodes are adjusted.
Iii. description of the legend
To build the initial heap:
Then, swap the elements of the heap top and the last element, at this point the last position as an ordered area (the ordered area is shown in yellow), and then the other unordered area of the heap adjustment, after re-get the Big top heap, swap the top of the heap and the location of the second penultimate element ...
Repeat this process until an ordered sequence is obtained
Iv. implementation of the Code
1 Private Static voidHeapsort (int[] a) {2 intN=a.length;//Get Array length3 intTemp//for exchanging data4 //Build the initial heap5 for(inti = N/2; I >=0; i--) {6Adddatetoheap (a,i,n-1);7 }8 System.out.println ();9System.out.print ("Original heap:");Ten for(inti = 0; i < a.length; i++) { OneSystem.out.print (a[i]+ ""); A } - System.out.println (); - for(inti = n-2; i>=0; i--) { the //row The tree root into an ordered sequence and continue building the heap -Temp=a[i+1]; -A[i+1]=a[0]; -a[0]=temp; +Adddatetoheap (A, 0, i); - } + } A at Private Static voidAdddatetoheap (int[] A,intIintN) { - - intTemp=a[i];//Record parent Node - intpost=0;//to determine if the parent node is greater than the child node - intJ=2*i;//Record child nodes - while(j<=n&&post==0){ in if(j<N) { - if(A[j]<a[j+1]) {//finding the maximum node toJ + +; + } - } the if(Temp>=a[j]) {//End the comparison process if the root is larger *Post=1; $}Else {Panax NotoginsengA[J/2]=A[J];//The tree roots are smaller and continue to compare -j=2*J; the } +a[j/2]=temp; A } the } + Public Static voidMain (string[] args) { - int[]a=New int[10]; $Random ran=NewRandom (); $System.out.println ("Pre-order Array"); - //use the random method to generate a random array and output - for(inti = 0; i < a.length; i++) { theA[i]=ran.nextint (100); -System.out.print (a[i]+ "");Wuyi } the //Call Sort Method - Heapsort (a); Wu //output post-sorting method -System.out.println ("sorted array"); About for(inti = 0; i < a.length; i++) { $ -System.out.print (a[i]+ ""); - } -}
View Code
Five, algorithm analysis
- All cases have a time complexity of O (Nlogn).
- Heap ordering is an unstable sort method.
- Just one extra space, the space is complex O (1).
Data Structure-Heap ordering (using Java)