Heap sort algorithm Java Implementation and Analysis

1, brief heap sorting algorithm

As the name suggests Kazakhstan, heap sort algorithm is to use heap An Algorithm for this data structure design, heap sort algorithm on the Internet can find a lot of information to explain, but I'm learning to their own understanding of their own to write a blog, so I do only critical point in my knowledge of some of the blog.

The key knowledge points heap sorting algorithm:

  • Time complexity: at the best, the worst, the time complexity of the average case is O (nlogn);
  • Stability: is an unstable sorting algorithm
  • Stack and heap into large root rootlets heap, the heap sort algorithm corresponding heap sort algorithm is divided into large and small root root heap sort algorithm
  • Large root heap sort algorithm corresponds to the ascending sort, heap corresponds rootlets descending sorting algorithm

2, the basics of the heap sorting algorithm

  Heap sort algorithm requires the use of such a heap data structure.

  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. Binary tree of knowledge about what is a complete binary tree is a full binary tree, etc. What is the basis of this foundation.  

  Because the heap is a complete binary tree, so we can use the storage array when complete binary tree, this is because the use of arrays can reflect the relationships between the various binary tree node array subscript. In this way we find a node around a child can be obtained directly array subscript child nodes according to the node's array index. In other words the logical binary tree structure can be indirectly reflected by the array index.

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

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

3, the basic idea of ​​the heap sorting algorithm

  • Step a: Construction of large root heap or rootlets reactor: Because of the array of arrays beginning is not consistent with the stack of characteristics, it requires a corresponding relationship subscripts between appropriate adjustment data in the array so as to satisfy meet large root heap or rootlets stack characteristics, when the adjustment is the last one by one upward from one non-leaf node starting the adjustment.
  • Step two: the end of the top of the stack elements and switching elements, so that the maximum end elements (for example using a large heap root). After the exchange is equivalent to the last element of the already good order. Because the array is large root heap, so the largest element in the end, the end result is a sort in ascending order of
  • Step three: performing a first element of the stack does not necessarily meet the End step two heap sort of characteristics, the first node needs to continue adjusting the stack, then the top of the stack elements and switching elements at the end, to give a second large element.
  • Step four: so repeatedly exchanged, rebuild, exchange.

 

4, Java heap sort algorithm to achieve

 

. 1  Package com.baozi.paixu;
 2  
. 3  Import java.util.Arrays;
 . 4  
. 5  / ** 
. 6  * HEAPSORT basic idea: collating sequence to be configured into a large heap root, at this time the maximum value is the top of the stack of the entire sequence root node. Element with which the end of the exchange,
 7  * In this case the end element is the maximum, then the element n-1 caused a re dog heap, it would receive a maximum of n-1 elements, so that the cycle can be obtained
 8  * an ordered sequence.
9  * Note: The best, the worst, are the average time complexity of O (nlogn) heap sort algorithm.
 10  * the step of sorting algorithms:
 11  * 1, the initial configuration of the stack: the sequence of a given configuration disordered into a large heap root (root pile with large ships ascending, descending stack with small root)
 12 is  * ① determined from the first sequence (array storage) the last non-leaf nodes (length / 2-1) constantly oriented from right to left under adjustment
 13 is  * ② ensure that each subtree is the largest subtree of the root node for the
 14  * 2, the end of the top of the stack elements and switching elements, such that the largest end of the element, and then the remaining n-1 elements continue continue to adjust n-1 is the number of elements
 15 * Big heap root, then this time the top of the stack element is taken out, so that the cycle can be obtained an ordered sequence.
16  *
 . 17  * @author baozi
 18 is  * @Create 2019-05-15-18: 16
 . 19   * / 
20 is  public  class HeapSort {
 21 is      public  static  void main (String [] args) {
 22 is          int [] = the nums new new  int [] 10 {,. 9,. 8,. 7,. 6,. 5,. 4,. 3, 2,. 1 };
 23 is          HeapSort.heapSort (the nums);
 24          System.out.println (of Arrays.toString (the nums));
 25  
26 is      }
 27  
28      public  static  voidheapSort ( int [] the nums) {
 29          // . 1, create a large root stack, both the random element as required to create a large root heap stack
 30          // since the beginning of the data element is present in the array
 31          // the aim here is to use the cycle for each non-leaf node must be individually adjusted 
32          for ( int I = nums.length / 2 -. 1; I> = 0; i-- ) {
 33 is              // adjusted when a large heap root is for the last non-oriented leaf node (for purposes of binary tree structure), from
 34              @ from right to left (for the purposes of storage array) restructuring 
35              adjustHeap (the nums, I, nums.length);
 36          }
 37          // 2, the end of the switching element and the first element and then re-adjust the stack structure 
38 is          for ( int J = nums.length -. 1; J> 0; J, ) {
39              // the top of the stack elements and switching elements at the end 
40              the swap (the nums, 0 , J);
 41 is              // re-adjustment of the heap 
42 is              adjustHeap (the nums, 0 , J);
 43 is          }
 44 is      }
 45  
46 is      Private  static  void the swap ( int [] the nums, int I, int J) {
 47          int TEMP = the nums [I];
 48          the nums [I] = the nums [J];
 49          the nums [J] = TEMP;
 50      }
 51 is  
52 is      Private  static void adjustHeap ( int [] the nums, int i, int length) {
 53 is          // first remove the current element i 
54 is          int TEMP = the nums [i];
 55          // from node i left child node starts, i.e. 2i + 1 at the start, in fact, this cycle is adjusted to direct the node to determine the final position 
56 is          for ( int K = 2 * I + 1; K <length; K = K * 2 + 1 ) {
 57 is              // If the left child point less than the right child node, k refers to the right child node 
58              IF (K +. 1 <length && the nums [K] <the nums [K +. 1 ]) {
 59                  K ++ ;
 60              }
 61 is              // If the child node is greater than a parent node, the value assigned to the parent node of the child node (no exchange) 
62             IF (the nums [K]> temp) {
 63 is                  the nums [I] = the nums [K];
 64                  I = K;
 65              }
 66          }
 67          // temp is a value into the final position 
68          the nums [I] = temp;
 69      }
 70  
71 is }

 

Guess you like

Origin www.cnblogs.com/BaoZiY/p/10929438.html