Heap sorting and heap sorting algorithms

Source: Internet
Author: User

Heap sorting and heap sorting algorithms

1 # include <iostream> 2 using namespace std; 3 int heap [101]; 4 int heap_size; 5 void put (int d) // heap [1] inserts 6 {7 int now, next; 8 heap [++ heap_size] = d; 9 now = heap_size; 10 while (now> 1) 11 {12 next = now/2; // parent node 13 if (heap [now]> = heap [next]) 14 break; 15 swap (heap [now], heap [next]); 16 now = next; 17} 18} 19 int get () // heap [1] obtain 20 {21 int now = 1, next, res = heap [1]; 22 heap [1] = heap [heap_size --]; // retrieve the end element 23 while (now * 2 <= heap_size) 24 {25 next = now * 2; // next left child 26 if (next 

Stl

 1 #include<iostream> 2 #include<algorithm> 3 #include<cstdio> 4 using namespace std; 5 int a[10000001]; 6 int main() 7 { 8     int n; 9     cin>>n;10     for(int i=1;i<=n;i++)11     {12         //cin>>a[i];13         scanf("%d",&a[i]);14     }15     make_heap(a+1,a+n+1);16     sort_heap(a+1,a+n+1);17     for(int i=1;i<=n;i++)18     {19         //cout<<a[i]<<endl;20         printf("%d\n",a[i]);21     }22     return 0;23 } 

 

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.