Select class sorting-heap sorting

Source: Internet
Author: User

# Include

 
  
# Define N 20int main () {void heapSort (int *, int); int I, a [N + 1]; printf ("Enter twenty numbers: \ n "); for (I = 1; I <= N; I ++)/* because the heap sorting uses a Complete Binary Tree, therefore, the storage of elements must start from 1 */scanf ("% d", a + I); heapSort (a, N); for (I = 1; I <= N; I ++) printf ("% d", a [I]); return 0 ;} /* This function completes the adjustment of nodes on the low position in the range from array a [low] to a [high] */void Sift (int * a, int low, int high) {int I = low, j = I * 2;/* a [j] is the left child node of a [I] */int temp = a [I]; /* Save the node on low */while (j <= high) {if (j

  
   
= 2; -- I)/* perform n-1 cycles to complete heap sorting */{temp = a [1];/* replace the elements in the root node with the following three sentences, place it in the final location */a [1] = a [I]; a [I] = temp; Sift (a, 1, I-1 ); /* this time, you only need to adjust the root node, and the number of elements is reduced by 1 */}}

  

 

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.