Heap-Heap Sort

Source: Internet
Author: User

    Heap ordering, like the merge sort, has a time complexity of O (NLGN), as with the insertion sort, with the spatial site: at any time, only a constant number of additional element space is required to store temporary data. The (binary) heap is an array that can be seen as an approximate complete binary tree with each node in the tree corresponding to an element in the array. In addition to the bottom level, the tree is fully filled and is padded from left to right. The array A that represents the heap has two properties: A.length is the number of elements in the array, and A.heap-size indicates how many heap elements are stored in the arrays. The root node of the tree is a[0], easy to get the parent node, left child and right child subscript:

The binary heap is divided into the largest heap and the smallest heap. The value of the node must satisfy the nature of the heap sequence, the largest element in the heap is stored in the root node, and in either subtree, the subtree contains no more than the value of the subtree node, and the minimum heap property is the opposite.
The basic process of the heap:
Max-heapify process: The time complexity is O (LGN), is the key to maintain the maximum heap properties.
Build-max-heap process: With linear time complexity, the function is to construct a maximum heap from an unordered array of input data.
Heapsort process: The time complexity is O (NLGN), the function is to a data in situ sorting.
Max-heap-insert, Heap-extract-max, Heap-increase-key, and heap-maximum processes: The time complexity is O (LGN), the function is to use the heap to implement a priority queue.

The input of the max-heapify is an array A and a subscript I, by letting the value of a[i] "step down" in the maximum heap, so that the following subscript i is the root node of the sub-tree to re-follow the nature of the maximum heap, can be achieved by recursion and non-recursive two ways, the code is as follows:
1 void 2Percdownrecursion (intA[],intIintN//recursive mode of maximum heap properties under max-heapify Filter Maintenance3 {4     intleft =Left (i);5     intright =Right (i);6     intlarge;7     if(Left < n && A[left] >A[i])8Large =Left ;9     ElseTenLarge =i; One     if(Right < n && A[right] >A[large]) ALarge =Right ; -     if(Large! =i) { - swap (A[i],a[large]); the percdownrecursion (a,large,n); -     } -}
1 voidPercdown (intA[],intIintN//non-recursive mode of maximum heap properties under max-heapify Filter Maintenance2 {3         intChild ;4     inttmp;5     6      for(TMP = A[i]; Left (i) < n; i =Child )7     {8Child =Left (i);9         if(Child! = N1&& A[child +1] >A[child])Tenchild++; One         if(TMP <A[child]) AA[i] =A[child]; -         Else -              Break; the     } -A[i] =tmp; -}

To build a heap, you can use the bottom-up method of the process max-heapify to convert an array of size n to the largest heap, A[N/2] The element is the leaf node of the tree, each leaf node can be seen as a heap containing only one element, the time complexity of O (n).

1 void buildmaxheap (intint  N)2{3for     ( int i = n/20//  build heap builds maximum heap 4         Percdownrecursion (a,i,n); 5 }

Heap sorting, the heap sorting algorithm uses BUILD-MAX-HEAP to build the largest heap of input array A, because the largest element in the array is always in the root node a[0], by exchanging it with a[n-1], you can put the element in the correct position, reduce the size of the heap and filter, thus in the a[0 ... N-2] to construct a new maximum heap. The heap sorting algorithm repeats this process until the heap size drops from n-1 to 1.

1 void2Heapsort (intA[],intN)3 {4 buildmaxheap (a,n);5      for(inti = n1; i >0; i--)6     {7Swap (a[0],a[i]); 8Percdownrecursion (A,0, i);//or call Percdown (a,0,i)9     }Ten}

Example:

1 intMain ()2 {3    intA[] = { the, -, -, -, A, -, to};4    intSize =7;5 Heapsort (a,size);6     for(intI=0; i < size; ++i)7printf"%d", A[i]);8printf"\ n");9}

Output:

Heap-Heap Sort

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.