Decoding heap sorting algorithm and using C + + to implement heap sort based on maximum heap _c language

Source: Internet
Author: User

1. Heap Sort Definition
N-keyword sequences kl,k2,...,kn called heaps, when and only if the sequence satisfies the following properties (referred to as heap properties):
(1) Ki≤k2i and Ki≤k2i+1 or (2) ki≥k2i and ki≥k2i+1 (1≤i≤)
If the vector stored by this sequence is R[1..N] as a storage structure of a complete binary tree, the heap is essentially a complete binary tree that satisfies the following properties: None of the key words in the tree is greater than (or not less than) the key word for its left and right children (if any).
The "example" keyword sequence (10,15,56,25,30,70) and (70,56,30,25,15,10) meet the heap properties (1) and (2) respectively, so they are all heaps, and their corresponding complete binary trees are the same as the minimum heap example and the maximum heap example.
Heap Sorting algorithm

2, maximum heap and minimum heap
(1) The key word for a root node (also called a heap top) is the heap called the minimum heap, which is the smallest of all the node keywords in the heap.
(2) The keyword of a node (also known as a heap top) is the largest of all the node keywords in the heap, called the maximum heap.
Attention:
(1) Any subtree in the heap is also a heap.
(2) The heap discussed above is actually a two-fork heap (Binary Heap), which similarly defines a K-fork heap.

3, the basic idea of heap sorting is as follows:
(1) The array to be sorted into a maximum heap
(2) to remove the root of the tree (maximum (small) value, the actual algorithm implementation is not really removed)
(3) construct the remaining elements in the tree into a maximum heap (the structure here is different from the 1th step, look at the implementation section)
(4) Repeat the 2,3 operation until all the elements are removed
(5) to arrange the elements in the order in which they are taken out, that is, to get an ordered array (in the code implementation is through the exchange operation " Virtually "finished"
begin to realize the algorithm first look at a few conclusions (prove slightly):
(1) Any node in the complete binary tree a[0:n-1], subscript II, and the subscript for its child nodes is 2i+12i+1 and 2 (i+1) 2 (i+1)
(2) The size of N of the complete binary tree a[0:n-1], the smallest leaf nodes are ⌊n2⌋⌊n2⌋, the most important non-leaf node is the ⌊n2⌋−1⌊n2⌋−1
(3) If the array is a maximum heap, then the maximum element is a[0]
(4) The left and right subtree of any node in the maximum heap is also the maximum heap
 
4, implementing example
the algorithm implementation here uses the largest heap, first resolving the problem of creating the largest heap by an array:

The subscript value for the two child nodes of the node with the subscript i is computed #define LEFT (i) (2 * (i) + 1) #define RIGHT (i) (2 * ((i) + 1))/* This function takes a subtree with node as its root in a binary tree
 into the largest heap.
 * Note: The prerequisite for use is the node's left and right subtree (if any) is the largest heap.
 * This function is the key to the whole algorithm.
  /void max_heapify (int heap[], int heap_size, int node) {//The problem of integer overflow is not considered here//first focus on main function///If the data is large enough, the type of int must overflow
  int l_child = Left (node);
  int r_child = right (node);
 
  int max_value = node;
  if (L_child < heap_size && Heap[l_child] > Heap[max_value]) {max_value = L_child;
  } if (R_child < heap_size && Heap[r_child] > Heap[max_value]) {max_value = R_child;
 
    } if (max_value!= node) {swap_val (heap + node, heap + max_value);
  Then make sure that the child of the swapped nodes is still the maximum heap//If not this node will continue to "sink" until the appropriate position max_heapify (heap, heap_size, max_value); }/* Make an array of maximum heap * bottom-up using the max_heapify function to process/void build_max_heap (int heap[], int heap_size) {if (Heap_size < 2
  ) {return; } int first_leaf = heap_size >> 1;//First leaf node subscript iNT I; Starting from the bottom of the last non-leaf node, the//leaf nodes are considered the largest heap, so you can use the Max_heapify function for (i = first_leaf-1 i >= 0; i--) {max_heapify (
  Heap, heap_size, i);
 }
}

function max_heapify "sinks" the root node of the specified subtree to the appropriate location, the final subtree becomes the largest heap, and the process has the worst time complexity of O (LOGN) O (log n). The function build_max_heap a bottom-up call max_heapify, eventually the entire array satisfies the maximum heap, and the iteration complexity is O (NLOGN) O (nlog N), so the worst time complexity of the entire function is also O (NLOGN) O (nlog N). And if the current array is already the largest heap, for example, the array is originally sorted in descending order, then the time complexity of the max_heapify process is O (1) O (1), at which point the time complexity of the BUILD_MAX_HEAP is O (n) o (n), which is the best case.

The heap sort process is then implemented:

/* Heap Sort main function
 */
void heap_sort (int heap[], int heap_size)
{
  if (heap = NULL | | heap_size < 2)
  {return
    ;
  }
  Construction
  of the largest heap build_max_heap (heap, heap_size);
 
  int i;
  for (i = heap_size-1 i > 0; i--) {/*
    Swap the root node of the current tree to the end
     * is equivalent to fetching the maximum value, the tree size is smaller.
     * The switched tree is not the largest heap, but the two subtrees of the root are still the maximum heap
     * satisfies the condition of invoking max_heapify. The reason for this exchange,
     * is because the max_heapify processing time is less complex,
     * If you do not exchange to "remove" heap[0], here you may want to use
     * build_max_heap to re-establish the largest heap, the time complexity of the larger
     * *
    swap_val (heap, heap + i);
 
    heap_size--;
    Maintain maximum heap
    max_heapify (heap, heap_size, 0);
  }

In the final heap-sorting algorithm, the complexity of the build_max_heap is known, the iterations are similar to the BUILD_MAX_HEAP implementations, and it is easy to see that the root element of the swap will inevitably sink to the bottom of the heap during the next build, so that, for better or worse, The time complexity of the iterative process is O (NLOGN) O (nlog N), so the best worst-case and mean time complexity for the entire algorithm is O (NLOGN) O (nlog N).
The spatial complexity of the heap sorting algorithm is O (1) O (1), which is easy to see from the implementation.

Related Article

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.