C ++ implements heap sorting

Source: Internet
Author: User

C ++ implements heap sorting

Heap sorting is a sort method that has the advantages of Merge Sorting and insert sorting. Its time complexity is O (nlgn), and it is also an in-situ Sorting Algorithm: at any time, only constant elements in the array are stored outside the input array. To introduce heap sorting, first introduce what is heap.

1. Heap Building: the heap data structure is an array object, which can be regarded as a Complete Binary Tree, such. The heap represented by the array on the right can be represented by a complete binary tree on the left. If the parent node corresponds to an array subscript of I, the array subscript of the left child is 2 * I, the right child is 2 * I + 1.



The Code is as follows: <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + signature + 0NC0 + signature/K/Signature + Cn08L3A + signature/CwLS96cnctPO4 + bbR0 + signature/CserOqmm1xMr91 + signature + vL/LXE1/Signature + jrNXSs/Signature + 2xhcmdlc3THobrD0 + tpz + tests/CserOqmxhcmdlc3S6zWnL + bbU06a1xMr91 + tests/eho9Xiw7TLtb/tests/tNXiuPa5/tests = "" alt = "\">




We can see that the process of retaining the root heap of nodes with the node value 4 in the figure. The Code is as follows:

Void max_heapify (int a [], int I, int heapSize ){
Int lindex = 2 * I, rindex = 2 * I + 1, largest = 0;
If (lindex <= heapSize & a [lindex-1]> a [I-1]) // identify who is the value of left child and node I, save the large worthy subscript as largest
Largest = lindex;
Else
Largest = I;

If (rindex <= heapSize & a [rindex-1]> a [largest-1]) // determine the value of the right child and node largest
Largest = rindex;

If (largest! = I) {// If largest is not equal to I
Swap (a [I-1], a [largest-1]); // exchange
Max_heapify (a, largest, heapSize); // continue to "maintain the root heap nature" for nodes whose subscript is largest and whose size is heapSize"
}
}

The following are swap functions:

Void swap (int & first, int & second ){
Int tem = 0;
Tem = first;
First = second;
Second = tem;
}

3. Heap Sorting Algorithm: I will use a chart to express it intuitively!


The process is to swap the root node of the large root heap with the last leaf node, then narrow down the array range (to reduce the heapSize), and eliminate the maximum value, finally, the new root node is "heap-preserving method", and so on. The Code is as follows:


Void heapSort (int a [], int heapSize ){
BuildMaxHeap (a, heapSize); // heap Creation
For (int I = heapSize; I> = 2; -- I ){
Swap (a [0], a [I-1]); // swap the root node with the final leaf node
-- HeapSize; // narrow the heap range.
Max_heapify (a, 1, heapSize); // preserve the heap nature

}
}

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.