Heap sequencing Practice

Source: Internet
Author: User

Today I study the heap sort, found a problem, you certification he is very simple you do not seriously difficult. Intentions to see any algorithm are very attractive, the previous review of the time to feel all the algorithm is back, this review feel very cool all is by understanding to deal with; I'll make a small record of my simple understanding

1. Build a heap of data first, here we choose Dagen (that is, the value of each node is not more than the value of its parent node).

The specific steps to be processed are starting from the first non-leaf node of the tree, usually starting from the N/2 node, if 2*n<=n is 2*n is its left child node, if 2*n+1 <=n.

From the N/2 node, compare the larger values of the N/2 node with its left and right child nodes, and if the values of the child nodes are greater than the values of the parent node, then the values of both are exchanged.

After processing the node, repeat the above steps on the next non-leaf node, and reorganize the adjustment if the data exchange situation causes the heap to be corrupted. Until all the nodes are satisfied.

2. After the above processing, the following start sorting, mainly to the last sub-node of the heap n-1 data and the root node 0 of the data swapped, after the swap we got a new destroyed heap and sorted array n, continue to follow step 1 of the process to readjust the destroyed heap, After the adjustment is completed, the last data of the latest heap is n-2 and the data of the root node 0 is swapped, ... Always repeat until you get our sorted array [a0 A1 A2 ... n].

Here's a code I wrote.

voidHeapsort (intAintN) {     for(inti = n/2-1; I >=0; i--)//starting from the node of the first non-leaf node of the tree, the node ID of the first non-leaf node is N/2    {         while(2* i +1< n)//has left node        {            intj =2* i +1; if(j +1< n) && (A[j] < A[j +1]))//has right node, while right node is greater than left node            {                ++J; }            if(A[i] <A[j])            //Exchange data for parent and child nodes {                intt =A[i]; A[i]=A[j]; A[J]=T; I=J;            //Heap is destroyed, re-organized } Else            {                 Break; }        }    }    //after the adjustment has been finished processing     for(inti = n-1; i >0; i--)    {        intt =A[i]; A[i]= a[0]; a[0] =T; //Exchange finished, start re-adjustment        intK =0;  while(2* k +1<i) {intj =2* k +1; if(j +1) < I && A[j] < A[j +1])            {                ++J; }            if(A[k] <A[j]) {                intt =A[k]; A[K]=A[j]; A[J]=T; K=J; }            Else            {                 Break; }        }    }}#defineMax_len 10int_tmain (intARGC, _tchar*argv[]) {    int*a =New int[Max_len]; Memset (A,0, Max_len);    Srand (time_t (NULL));  for(inti =0; i < Max_len; i++) {A[i]= rand ()% -; printf ("%d", A[i]); } printf ("\ n"); printf ("start sort \ n"); intTick =GetTickCount (); Heapsort (A, Max_len); printf ("\ n"); printf ("Sort by:%d\n", GetTickCount ()-tick);  for(inti =0; i < Max_len; i++) {printf ("%d", A[i]); } System ("Pause"); return 0;}

Heap sequencing Practice

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.