-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path20_merge_sort.cpp
74 lines (63 loc) · 1.48 KB
/
20_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
/*Merge Sort
TC = O(nlogn)
SC = O(n)*/
#include<bits/stdc++.h>
using namespace std;
void merge(vector<int> &arr, int low, int mid, int high){
//2 halves ->> [low....mid] and [mid+1....high]
int left = low; //starting index of left half of array
int right = mid + 1; //starting index of right halfof array
vector <int> temp; //temporary array
while(left <= mid && right <= high) {
if(arr[left] < arr[right]) {
temp.push_back(arr[left]);
left++;
}
else{
temp.push_back(arr[right]);
right++;
}
}
//push left-out elements to temp array from left half
while(left <= mid) {
temp.push_back(arr[left]);
left++;
}
//push left-out elements from right half
while(right <= high) {
temp.push_back(arr[right]);
right++;
}
//put elements of temp array to original array arr
for(int i=low; i<=high; i++) {
arr[i]= temp[i - low];
}
}
void merge_sortt(vector<int> &arr, int low, int high) {
//base case
if(low>=high) { //one element doesnt need sorting
return;
}
int mid = (low + high) /2;
//left half recursion
merge_sortt(arr, low, mid);
merge_sortt(arr, mid + 1, high);
//right half recursion
merge(arr, low, mid, high); //merging sorted halves
}
int main() {
int n;
cin>>n;
int a;
//insert elements into the array
vector <int> arr;
for(int i=0; i<n ;i++){
cin>>a;
arr.push_back(a);
}
//arr, low, high
merge_sortt(arr, 0, n-1);
for(int i=0; i<n ;i++) {
cout<<arr[i]<<" ";
}
}