-
Notifications
You must be signed in to change notification settings - Fork 0
/
merge_sort.cpp
69 lines (66 loc) · 1.05 KB
/
merge_sort.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
60
61
62
63
64
65
66
67
68
69
#include<iostream>
using namespace std;
void show(int a[],int i,int n){
for(int j=i;j<n;j++)
cout<<a[j]<<" ";
cout<<endl;
}
void in(int a[],int n){
for(int i=0;i<n;i++)
cin>>a[i];
}
void print(int a[],int n){
for(int i=0;i<n;i++)
cout<<a[i];
}
void merge(int a[],int l,int m,int r){
int a1[m-l+1];
int a2[r-m];
int k=0;
for(int i=0;i<=m-l;i++)
a1[i]=a[i+l];
k=0;
//show(a1,)
for(int i=0;i<r-m;i++)
a2[i]=a[i+m+1];
int j=0,o=0;
k=l;
// show(a2,0,m-l);
while(j<m-l+1 && o<r-m){
if(a1[j]>=a2[o])
a[k]=a2[o++];
else
a[k]=a1[j++];
k++;
}
while(j<=m-l){
a[k]=a1[j];
k++;
j++;
}
while(o<r-m){
a[k]=a2[o];
k++;
o++;
}
// show(a,l,r+1);
}
void merge_sort(int a[],int l,int n){
//int r=n-1;
if(l>=n)
return;
int m=l+(n-l)/2;
merge_sort(a,l,m);
merge_sort(a,m+1,n);
merge(a,l,m,n);
}
int main(void){
int n;
cout<<"Size of the array-";
cin>>n;
int a[n];
cout<<"ENTER ELEMENTS-";
in(a,n);
merge_sort(a,0,sizeof(a)/sizeof(a[0]));
show(a,0,n);
}