-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmergeSort.cpp
More file actions
49 lines (44 loc) · 992 Bytes
/
mergeSort.cpp
File metadata and controls
49 lines (44 loc) · 992 Bytes
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
#include<bits/stdc++.h>
using namespace std;
void mergeSort(int arr[],int low,int high){
if(low==high)return;
int mid=(low+high)/2;
mergeSort(arr,low,mid);
mergeSort(arr,mid+1,high);
merge(arr,low,mid,high);
}
void merge(int arr[],int low,int mid,int high){
vector<int> v;
int left=low;
int right = mid+1;
while(left<=mid && right<=high){
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<=high){
v.push_back(arr[right]);
right++;
}
for(int i=low;i<=high;i++){
arr[i]=v[i-low];
}
}
int main(){
int arr[]={5,4,3,2,1};
int l=5;
mergeSort(arr,0,l-1);
for(int i=0;i<l;i++){
cout<<arr[i]<<" ";
}
return 0;
}