#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