Sorting method Summary (i) heap sorting

Source: Internet
Author: User

The maximum heap (binary) sorting is divided into several steps:

1.maxheap (), maintaining the nature of the maximum heap, that is, the value of the node is greater than the value of the child node, time complexity O (LGN)

2.bulid_max_heap (), constructs the largest heap from an unordered array, time complexity O (n)

3.heap_sort (), sorting unordered array, time complexity O (NLGN)

Code has comments

1#include <iostream>2 usingstd::cout;3 4 voidMax_heapify (int*a,intLeninti)5 {6     //array starting with 0, left child for 2i+1, right child for 2i+27     intL=2*i+1, r=2*i+2, largest;8 9     //Select the maximum value, record its pointer to largestTen     if(L<len && a[l]>a[i]) largest=l; One     Elselargest=i; A     if(R<len && a[r]>a[largest]) largest=R; -     if(largest!=i) -     { the{inttemp=a[i];a[i]=a[largest];a[largest]=temp;} -  -         //The node is changed, so it is necessary to heapify the changed nodes. -         //because Heapify is performed after its child node heapify. + max_heapify (a,len,largest); -     } + } A voidBulid_max_heap (int*a,intlen) at { -     //len/2,len/2+1 is a leaf node, not heapify . -      for(inti=len/2; i>=0; i--) max_heapify (a,len,i); - } - voidHeapsort (int*a,intlen) - { in     inttemp; - bulid_max_heap (A,len); to      for(inti=len-1; i>=1; i--) +     { -         //Place the top of the heap (maximum) at the end of the array the{temp=a[0];a[0]=a[i];a[i]=temp;} *          $         //the array length -1,heapify the root node and becomes the largest heapPanax Notoginsenglen--; -Max_heapify (A,len,0); the     } + } A  the  + intMain () - { $     inta[]={ A,8564,445, About,8, Wu,6,4,5,4}; $Heapsort (A,Ten); -      for(intI=0;i<Ten; i++) cout<<a[i]<<"  "; -  the     return 0; -}

Sorting method Summary (i) heap sorting

Large-Scale Price Reduction
  • 59% Max. and 23% Avg.
  • Price Reduction for Core Products
  • Price Reduction in Multiple Regions
undefined. /
Connect with us on Discord
  • Secure, anonymous group chat without disturbance
  • Stay updated on campaigns, new products, and more
  • Support for all your questions
undefined. /
Free Tier
  • Start free from ECS to Big Data
  • Get Started in 3 Simple Steps
  • Try ECS t5 1C1G
undefined. /

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.