-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMerge_sort.cpp
More file actions
59 lines (51 loc) · 1.1 KB
/
Merge_sort.cpp
File metadata and controls
59 lines (51 loc) · 1.1 KB
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
// Sorting technique : Merge sort
// Time Complextity : Best case[O(nlogn)] Average case[O(nlogn)] Worst case[O(nlogn)]
// Space Complexity : O(n)
// Stable/Unstable : Stable
#include<bits/stdc++.h>
using namespace std;
void Merge(vector<int>&arr,int low,int mid,int high){
int l_sz = mid-low+1;
int h_sz = high-mid;
vector<int>l(l_sz+1,INT_MAX);
vector<int>h(h_sz+1,INT_MAX);
for(int i=0; i<l_sz; ++i){
l[i] = arr[low+i];
}
for(int i=0; i<h_sz; ++i){
h[i] = arr[mid+1+i];
}
int p=0,q=0;
for(int i=low; i<=high; ++i){
if(l[p] <= h[q]){
arr[i] = l[p];
p++;
}
else{
arr[i] = h[q];
q++;
}
}
}
void MergeSort(vector<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);
}
int main(){
int n;
cin>>n;
vector<int>arr(n); // declaring an array of size n
for(int i=0; i<n; ++i){
// taking input
cin>>arr[i];
}
int low = 0,high = n-1;
MergeSort(arr,low,high);
for(int i=0; i<n; ++i){
cout<<arr[i]<<" ";
}
return 0;
}