-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmergeSort.cpp
More file actions
49 lines (43 loc) · 907 Bytes
/
Copy pathmergeSort.cpp
File metadata and controls
49 lines (43 loc) · 907 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 <iostream>
#include <vector>
using namespace std;
void mergeSort(arr[], st, end){
If (st < end) {
int mid = st + (end-st)/2;
mergeSort(arr, st, mid);
mergeSort(arr, mid+1, end);
merge(arr, st, mid, end);
}
}
void merge(arr, st, mid, end){
vector<int>temp;
int i=st, j=mid+1;
while(i <= mid && j <= end){
if(A[i] <= A[j]){
temp.push_back(A[i]);
i++;
}else {
temp.push_back(A[j]);
j++;
}
}
}
// 𝘓𝘦𝘧𝘵
while(i <= mid){
temp.push_back(A[i]);
i++;
}
// 𝘙𝘪𝘨𝘩𝘵
while(j <= mid){
temp.push_back(A[j]);
j++;
}
for(idx = 0; idx < temp.size( ); idx++){
A[idx + st] = temp[idx];
}
int main(){
// int arr[] = {12, 11, 13, 5, 6,}
// int n = sizeof(arr)/sizeof(arr[0]);
// mergeSort(arr, 0, n-1);
return 0;
}