Introduction to Algorithms (II.) heap sequencing

Source: Internet
Author: User

Debug So Long hhhhhhh

Words not much to say the sticker code


It is entirely possible to say that Exchange is written as a separate function to reduce the amount of code reuse;

#include <iostream> #include <cstdlib>using namespace std;void max_heapify (int *a,int i,int _size) {int l,r,    largest;    int temp;    L = 2*i;    R = 2*i+1;    if (l<=_size && a[l]>a[i]) largest=l;    else largest=i;    if (r<=_size && a[r]>a[largest]) largest=r;        if (largest!=i) {temp=a[i];        A[i]=a[largest];        A[largest]=temp;    Max_heapify (a,largest,_size);    }}void build_max_heap (int *a,int _size) {int i=0; for (i=_size/2;i>=1;i--) max_heapify (a,i,_size);}    void Heap_sort (int *a,int length) {int _size=length;    Build_max_heap (a,_size);    int temp;        for (int i=length;i>=2;i--) {temp=a[1];        A[1]=a[i];        A[i]=temp;        _size--;    Max_heapify (a,1,_size);    }}int Main () {int a[6];    for (int i=0; i<6; i++) {cin>>a[i];    } heap_sort (a,5);    for (int i=0; i<6; i++) {cout << a[i] << "; } return 0;} 


233333333333333333333333333333 haha haha haha


Automated testing can be done with the rand () function of stdlib.h

Hum-hum ~~~~~~

Introduction to Algorithms (II.) heap sequencing

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.