-
Notifications
You must be signed in to change notification settings - Fork 0
/
bottomupMergesort.cpp
59 lines (45 loc) · 1.23 KB
/
bottomupMergesort.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
@chiranjeevi
Date: 14-May-17
#include <iostream>
#include <random>
using namespace std;
void merge(int arr[], int aux[], int lo, int mid, int hi){
for(int i=lo; i<=hi; i++)
aux[i]=arr[i];
for(int k=lo,i=lo,j=mid+1; k<=hi; k++){
if(i>mid)
arr[k]=aux[j++];
else if(j>hi)
arr[k]=aux[i++];
else if(aux[i]>aux[j])
arr[k]=aux[j++];
else
arr[k]=aux[i++];
}
}
void bottomupMergeSort(int arr[], int aux[], int n){
int sz=1;
while(sz<n){
for(int i=0; i+sz<n; i+=sz+sz) // // till mid+1<n to prevent redundant merge of arrays smaller the size sz
merge(arr,aux,i,min(i+sz-1,n-1),min(i+sz+sz-1,n-1)); //lo=i,mid=lo+(hi-lo)/2=i+(sz+sz-1)/2,hi=i+sz+sz-1
sz+=sz;
}
}
void print(int arr[], int n){
for(int i=0; i<n; i++)
cout<<arr[i]<<" ";
cout<<endl;
}
int main(){
int n,range;
cin>>n>>range;
int arr[n],aux[n];
default_random_engine gen;
uniform_int_distribution<int> dist(1,range);
for(int i=0; i<n; i++){
arr[i]=dist(gen);
}
print(arr,n);
bottomupMergeSort(arr,aux,n);
print(arr,n);
}