Implementing heap sequencing in C + +

Source: Internet
Author: User

A heap data structure is an array object that can be considered a complete binary tree structure.

Maximum heap: Each parent node is larger than the child node.

Minimum heap: Each parent node is smaller than the child node.

Heap sorting idea: For a given n data, initially consider these records as a sequential stored two-fork tree, then adjust it to a maximum heap, and then the last element of the heap with the top element of the heap (that is, the root node of the binary tree), the last element of the heap is the maximum record Then the (N-1) element (that is, excluding the largest record) is resized to a maximum heap, and then the top element of the heap is swapped with the last element of the heap to obtain a minor record, repeating the process until only one element is left on the adjusted heap, which is the smallest record, at which point an ordered sequence can be obtained.

Heap sorting consists of two processes: one is the build heap, the other is the position of the top element and the last element of the exchange heap.

The procedure is as follows:

#include <iostream> #include <assert.h>using namespace std;void adjustdown (int * Array,int size,int root) {assert (array);int child = 2 * root + 1; while  (child < size) {if  (Child + 1 < size&&array[child]  < array[child + 1]) {++child;} if  (Array[child]>array[root]) {swap (Array[root],array[child]);root = child;child =  2 * root + 1;} Else{break;}}} Void heapsort (int *array, int size, int root) {//Find first non-leaf node build heap for  (int  i =  (size - 2)  / 2; i >= 0; --i) {AdjustDown (array,size,0) ;} for  (int i = 0; i < size; ++i) {swap (array[0],array[size-1-i]); Adjustdown (array,size-1-i,0);}} Int main () {int array[10] = {12,10,16,6,8,9,11,9,7,13};for  (INT&NBSP;I&NBSP;=&NBSp;0; i < 10; ++i) {cout << array[i] <<  " ";} cout << endl; Heapsort (array,10,0);for  (int i = 0; i < 10; ++i) {cout < < array[i] <<  " ";} cout << endl;return 0;}

Program Run Result:

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M01/80/28/wKioL1c5tRzgitU-AAAR_VIXBbE682.png "title=" Heap.png "alt=" Wkiol1c5trzgitu-aaar_vixbbe682.png "/>


The heap ordering method is generally useful for files with fewer records, but it is very effective for files with more records, and its run time is mainly spent on creating heaps and repeatedly tuning the heap. Heap ordering even in the worst case, its time complexity is O (N*LOGN).

This article is from the "Zwy" blog, make sure to keep this source http://10548195.blog.51cto.com/10538195/1774049

Implementing heap sequencing in C + +

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.