-
Notifications
You must be signed in to change notification settings - Fork 0
/
merge sort.cpp
48 lines (38 loc) · 1.04 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
void merge(int arr[], int l, int mid, int r)
{
// Your code here
vector<int>v;
int left=l;
int right=mid+1;
while(left<=mid && right<=r){
if(arr[left]<=arr[right]){
v.push_back(arr[left]);
left++;
}
else{
v.push_back(arr[right]);
right++;
}
}
while(left<=mid) {
v.push_back(arr[left]);
left++;
}
while(right<=r) {
v.push_back(arr[right]);
right++;
}
for(int i=l; i<=r;i++) {
arr[i]=v[i-l];
}
}
public:
void mergeSort(int arr[], int l, int r)
{
//code here
if(l>=r) return ;
int mid=l+(r-l)/2;
mergeSort(arr,l,mid);
mergeSort(arr,mid+1,r);
merge(arr,l,mid,r);
}