Heap Data Structure and heap Sort

Source: Internet
Author: User

Reference: http://www.codeproject.com/Tips/732196/Heap-Data-Structure-and-Heap-Sort#xx4775794xx

#include <cstdio>//The sift function://' sifts down ' the A[l] item until heap properties is restored//ARGS://a[] (INOUT) the array (where a[l+1], a[l+2]: a[size-1] is a partial heap)//size (in) number of items of a//l (in) index of the item inserted into the heapvoidSiftintA[],intSizeintl) {    intI, J, X; I=l; J=2* i +1; X=A[i];  while(J <size) {        //The maximum of left and right child        if(J < Size-1)        if(A[j] < A[j +1])            ++J; if(x >=A[j]) Break; A[i]=A[j]; I=J; J=2* i +1;//Sift} A[i]=x;}//makes the heap using the R.w.floyd ' s algorithm//ARGS://a[] (INOUT) the array wherein the heap is created//size (in) the size of the arrayvoidMake_heap (intA[],intsize) {    intL = size/2;  while(l) {--l;    Sift (A, size, l); }}voidHeapsort (intA[],intsize) {    intL =0, r =size;    Make_heap (A, size);  while(R >1)    {        intTMP = a[0]; --R; a[0] =A[r]; A[R]=tmp; Sift (A, R,0); }}intMain () {inta[Ten] = {1,3,2,5, One, $,7,9, +,0 }; Heapsort (A,Ten);  for(inti =0; I <Ten; i++) {printf ("%d", A[i]); }    return 0;}

Heap Data Structure and 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.