Basic interaction design algorithm (8) - Heap Sort

  Heap sort is a tree selection sort, is effective in improving sort of direct selection.

  Stack is defined as follows: a sequence of n elements (k1, k2, ..., kn), if and only if the call stack. It can be seen defined by the stack, the top of stack elements (i.e., the first element) must be a minimum item (small top stack).

  In terms of a one-dimensional array memory stack, the stack corresponding to a complete binary tree, and the values ​​of all non-leaf node is not greater than (or less than) the value of its children, the value of the root (top of the stack element) is the smallest (or maximum). Such as:

  (A) the large top stack sequence: (96,83,27,38,11,09)

  (B) small pile top sequence: (12,36,24,85,47,30,53,91)

       The basic idea: When the initial sequence number of n binary tree is considered to be sorted (one-dimensional array storing binary) stored in a sequence, to adjust their storage order, making a stack, the stack top element of the output, to give n elements minimum (or maximum) of the elements, the minimum number of time heap root (or maximum). Then preceding (n-1) th element of the stack to become re-adjust the output stack top element to obtain n times smaller elements (or times larger) element. And so on, until only two nodes of the heap, and they make the exchange, has finally been ordered sequence of n nodes. This process is known as heap sort.

  Analysis time complexity: O (nlog (n)), is an unstable heap sort algorithm to sort.

  Therefore, to achieve heapsort need to solve two problems:
  1. How will the number of n to be the sort of built heap?
  2. Output top of the heap element, how to adjust the remaining n-1 elements, making it a new heap?

  First, the second issue: the top of the heap after the output element, how the remaining n-1 elements re-built heap?

  The method of adjusting the small stack top:
  1) with m elements stack, the top of the heap after the output element, the remaining m-1 elements. The top of the stack into the stack bottom element ((top of the stack and the last element of the exchange), the heap is broken, only the root node does not satisfy the reason of the nature of the stack.
  2) the root node of the left and the right subtree make minor elements to be exchanged.
  3) If the left subtree exchange: if the left subtree heap corruption, i.e., does not satisfy the left subtree root stack nature, repeat process (2)
  4) If the right subtree exchange, if the right subtree heap corruption, i.e. right subtree root node does not satisfy the properties of the stack. Repeat the process (2)
  5) is not satisfied continues heap property subtree performs said switching operation, until the leaf nodes, the stack is built.
  We call this process of adjustment from the root to leaf node for screening.

  Discuss the first question, how the n elements to be sorted initially built heap?

  Built heap Methods: during the initial sequence of built heap is a repeated process of screening.
  1) n-node binary tree, the node is the last one of the n / 2 nodes of the subtree.
  2) selecting from the second subtree n / 2 nodes of the root begins, the stack becomes subtree.
  3) After the subtree rooted at the forward successively be screened to each node, making the stack until the root node.

HEAPSORT:

void HeapSort ( int A [], int n-) {
   // initialize stack 
  HeapBuilding (A, n-);
   // start from the last node to adjust 
  for ( int I = n--. 1; I> 0; i-- ) {
     // switching element and the top of the stack last element 
    int TEMP = a [0 ]; 
    a [ 0] = a [I]; 
    a [I] = TEMP;
     // after each exchange must be adjusted 
    HeapAdjusting (a, 0 , I); 
  } 
}

Built heap:

void HeapBuilding ( int A [], int n-) {
   // from the last position of a child node of the start of adjustment, the position of a last child node is (n--. 1) / 2 
  for ( int I = (. 1-n-) / 2; I> = 0; i-- ) 
    HeapAdjusting (A, I, n-); 
}

Adjust the heap:

void HeapAdjusting ( int A [], int the root, int n-) {
   int TEMP = A [the root];
   int Child the root = 2 * +. 1; // left child position of 
  the while (Child < n-) {
     // find the child node the smaller of the 
    IF (child +. 1 <&& n-a [child +. 1] < a [child]) 
      child ++ ;
     // if the larger child nodes is less than the parent node, replacing the child node with a smaller parent and re-set at a parent and child nodes need to be adjusted. 
    IF (A [the root]> A [Child]) { 
      A [the root] = A [Child]; 
      the root = Child; 
      Child = 2 * + the root. 1 ;
    } The else 
      BREAK ;
     // adjustment value assigned to the parent node prior to the adjusted position. 
    A [the root] = TEMP; 
  } 
}

 

Guess you like

Origin www.cnblogs.com/x5115x/p/12637940.html