-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmergeSort1.cpp
65 lines (61 loc) · 1.03 KB
/
mergeSort1.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
#include <bits/stdc++.h>
#define ll long long
using namespace std;
void merge(int a[],int l,int m,int r){
int n1 = m-l+1;
int n2 = r-m;
int a1[n1];
int a2[n2];
int j=0;
for(int i=l;i<=m;i++){
a1[j]=a[i];
j++;
}
j=0;
for(int i=m+1;i<=r;i++){
a2[j]=a[i];
j++;
}
int i=0;
j=0;
int s=l;
while(i<n1 and n2<j){
if(a1[i]<a2[j]){
a[s]=a1[i];
i++;
s++;
}else{
a[s]=a2[j];
j++;
s++;
}
}
while(i<n1){
a[s]=a1[i];
i++;
s++;
}
while(j<n2){
a[s]=a2[j];
j++;
s++;
}
}
void mergeSort(int a[],int l,int r){
if(l<r){
int mid = (l+r)/2;
mergeSort(a,l,mid);
mergeSort(a,mid+1,r);
merge(a,l,mid,r);
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int v[] = {17,2,41,54,1};
mergeSort(v,0,4);
for(int i=0;i<=4;i++){
cout<<v[i]<<" ";
}
return 0;
}