Bottomupsort algorithm C + + code implementation

Source: Internet
Author: User

#include <iostream>using namespacestd;#defineN 100intA[n]; Static intN; voidInitial () {cout<<"Please enter the number of elements:"; CIN>>N; cout<<"Please enter"<< N <<"Elements of:";  for(inti =1; I <=n; i + +) {cin>>A[i]; }}voidPrint () {cout<<"after Bottomupsort:";  for(inti =1; I <=n; i + +) {cout<< A[i] <<" "; } cout<<Endl;}voidMerge (intA[],intPintQintR) {    intB[n]; ints = p, t = q+1, k =p;  while(s <= q && t <=r) {if(A[s] <=A[t]) {B[k+ +] = a[s++]; }        Else{b[k+ +] = a[t++]; }    }    if(s==q+1)    {         for(inti = t; I <= R; i + +) {b[k++] =A[i]; }    }    Else    {         for(intj = S; J <= Q; J + +) {b[k++] =A[j]; }    }    //copy the elements in b[] into a[]     for(inti = P; I <= R; i++) {A[i]=B[i]; }}voidBottomupsort (intA[],intN) {   intt =1; intS,i;  while(t<N) {s= T;t =2*s;i=0;  while(i+t<=N) {Merge (a,i+1, i+s,i+t); I= i+T; }        if(i+s<N) {Merge (a,i+1, i+s,n); }   }}intMain () {Initial (); if(N >1) {bottomupsort (a,n);    Print (); }    Else if(n = =1) {Print (); } System ("Pause"); return 0;}

Bottomupsort algorithm C + + code implementation

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.