-
Notifications
You must be signed in to change notification settings - Fork 53
/
merge sort.cpp
75 lines (73 loc) · 1.07 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
70
71
72
73
74
75
#include <iostream>
using namespace std;
void merge(int a[],int p,int q,int r)
{
int m=q-p+1;
int n=r-q;
int A[m],B[n];
for(int i=0;i<m;i++)
{
A[i]=a[p+i];
}
for(int i=0;i<n;i++)
{
B[i]=a[q+1+i];
}
int i, j, k;
i=0;j=0;k=p;
cout<<p<<"\n";
while(i<m&&j<n)
{
if(A[i]<=B[j])
{
a[k]=A[i];
i++;
}
else
{
a[k]=B[j];
j++;
}
k++;
}
while(i<m)
{
a[k]=A[i];
i++;
k++;
}
while(j<n)
{
a[k]=B[j];
j++;
k++;
}
}
void mergeSort(int a[],int l,int r) {
if (l < r) {
int m =l+(r-l)/2;
mergeSort(a,l,m);
mergeSort(a,m + 1,r);
merge(a,l,m,r);
}
}
void display(int n,int a[]){
for (int i=0;i<n;i++)
cout<<a[i]<<" ";
cout<<endl;
}
int main() {
int n;
cout<<"Enter size:";
cin>>n;
int a[n];
cout<<"Enter value:";
for(int i=0;i<n;i++)
{
cin>>a[i];
}
mergeSort(a,0,n-1);
cout<<"Sorted array:";
display(n,a);
return 0;
}