[Sorting algorithm] (9) heapsort

Heapsort


2019-11-10 11:45:11 by rushing

 

1, the concept of

An Algorithm, heap sort heap sort heap using this data structure is designed for a selection sort, its worst, preferably, the average time complexity are O (nlogn), which is also the sort of instability. First, a simple understanding of the structure under the heap.

stack

Heap is a complete binary tree with the following properties: the value of each node is equal to or greater than the left and right child node values, called the large top stack; each node or a value less than or equal to its left child node value, known as the small stack top. As shown below:

Meanwhile, we stack node are numbered layer, which maps the logical structure into an array will look like:

 

The array is logically a heap structure, we use a simple formula to describe the stack definition is:

大顶堆:arr[i] >= arr[2i+1] && arr[i] >= arr[2i+2]  

小顶堆:arr[i] <= arr[2i+1] && arr[i] <= arr[2i+2]  

 

 

2, the basic idea

The basic idea is to sort the stack: the collating sequence to be configured into a large pile top, at this time, the maximum value of the root node is the top of the stack of the entire sequence. To be exchanged with the last element on the end of this time the maximum value. The remaining n-1 th element of a stack is reconfigured, it would receive the next smallest value of n elements. So again executed, it will be able to get an ordered sequence of

A step  configuration initial heap. Given sequence disorder configured to stack a large top (top stack with large ships ascending, descending a small stack top).

1. Assuming a given sequence disorder following structure

 

2. At this point we have from the last non-leaf nodes start (natural leaf nodes without adjustment, the first non-leaf node arr.length / 2-1 = 5 / 2-1 = 1, which is below 6 knot point), from left to right, from the bottom is adjusted.

 

3. Locate the second non-leaf nodes 4, since [4,9,8] Maximum switching element 9, 9 and 4.

 

In this case, leading to the exchange sub-root [4,5,6] chaotic structure, continue to adjust the [4,5,6] Maximum 6, 4 and 6 exchange.

 

At this point, we will construct a sequence without having to become a top big heap.

Step two  The top of the stack elements and switching elements at the end of the last element is maximized. Then continue to adjust the stack, then the top of the stack elements and switching elements at the end, to give a second large element. So again exchanged, rebuild, exchange.

a. The end 9 and the top of the stack element exchange elements 4

b. re-adjust the structure, so that it continues to meet the definition of heap

 

C. Then the top stack element exchange with the end element 8 5, 8 to obtain a second large element.

 

Subsequent process, continue to adjust the exchange, so repeatedly, so that eventually the entire ordered sequence

 

And then briefly summarize the basic ideas under Heap Sort:

① The sequence need not build into a stack, in an ascending descending stack needs to choose a large top or top of a small stack;

② the top of the stack element exchange element to the end, the largest element will "sink" to the end of the array;

③ readjust structure, so as to satisfy the definition of the stack, and then continue to exchange with the current top of the stack elements of the last element, the adjustment is repeatedly executed + exchange step, until the entire sequence of ordered.

 

3, complete code

 1 public class HeapSort {
 2     public static void main(String []args){
 3         int i;
 4         int []a = {9,8,7,6,5,4,3,2,1};
 5 
 6         System.out.printf("before sort:");
 7         for (i=0; i<a.length; i++)
 8             System.out.printf("%d ", a[i]);
 9         System.out.printf("\n");
10 
11         heapSort(a);
12 
13         System.out.printf("after  sort:");
14         for(I = 0; I <a.length; I ++ )
 15              System.out.printf ( "% D" , A [I]);
 16          System.out.printf ( "\ n-" );
 . 17      }
 18 is      public  static  void heapSort ( int [] ARR) {
 . 19          // 1. Construction of the large top stack 
20 is          for ( int I = arr.length / 2-1; I> = 0; i-- ) {
 21 is              // from the first non-leaf node from bottom to top, right to left adjustment structure 
22 is              adjustHeap (ARR, I, arr.length);
 23 is          }
 24          // 2. adjust the top of the stack the stack structure + exchange element and the end element 
25          for ( int= arr.length. 1-J; J> 0; J, ) {
 26 is              the swap (ARR, 0, J); // the top of the stack elements and switching elements at the end 
27              adjustHeap (ARR, 0, J); // re-adjustment of the heap 
28          }
 29  
30      }
 31 is  
32      / ** 
33       * resizing the top of the stack (only the adjustment process, based on the large stack is built on top)
 34 is       * / 
35      public  static  void adjustHeap ( int [] ARR, int I, int length) {
 36          int TEMP = ARR [I]; // first remove the current element I 
37 [          for ( int= 2 * i + K 1; K <length; K = K * 2 + 1 ) {
 38 is              // starting from the left child node of the node i, i.e. 2i + 1 starts at 
39              IF (K + 1 <length && ARR [K] <ARR [K +. 1 ]) {
 40                  // If the left child node is less than the right child node, k refers to the right child node 
41 is                  K ++ ;
 42 is              }
 43 is              IF (ARR [K]> TEMP) {
 44                  // If the child node is greater than a parent node, a child node the value assigned to the parent node (no exchange) 
45                  ARR [I] = ARR [K];
 46 is                  I = K;
 47              } the else {
 48                  BREAK ;
 49              }
 50         }
 51 is          ARR [I] = temp; // the value of temp into its final position 
52      }
 53 is  
54 is      / ** 
55       * switching element
 56 is       * / 
57 is      public  static  void the swap ( int [] ARR, int A, int B) {
 58          int TEMP = ARR [A];
 59          ARR [A] = ARR [B];
 60          ARR [B] = TEMP;
 61 is      }
 62 is }
before sort:  9 8 7 6 5 4 3 2 1 
after  sort:  1 2 3 4 5 6 7 8 9 

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/yadiel-cc/p/11829407.html