-
Notifications
You must be signed in to change notification settings - Fork 0
/
ollie129_Merge-Sort.cpp
65 lines (64 loc) · 1.31 KB
/
ollie129_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
#include<bits/stdc++.h>
using namespace std;
// Fucking Merge Sort Yet Again !!!
void Merge_Sort(int arr[] , int start , int end)
{
if(start == end)
{
return;
}
int mid = (start + end)/2;
Merge_Sort(arr , start , mid);
Merge_Sort(arr , mid + 1 , end);
int s1 = start , s2 = mid + 1 , e1 = mid , e2 = end;
vector<int> ans;
while(s1 <= e1 && s2 <= e2)
{
if(arr[s1] <= arr[s2])
{
ans.push_back(arr[s1]);
s1++;
continue;
}
if(arr[s2] < arr[s1])
{
ans.push_back(arr[s2]);
s2++;
continue;
}
}
while(s1 <= e1)
{
ans.push_back(arr[s1]);
s1++;
}
while(s2 <= e2)
{
ans.push_back(arr[s2]);
s2++;
}
for(int i = 0 ; i < ans.size() ; i++)
{
arr[start + i] = ans[i];
}
return;
}
int main()
{
int size;
cout << "Enter the size of the array: ";
cin >> size;
int arr[size];
cout << "Enter the elements of the array: " << endl;
for(int i = 0 ; i < size ; i++)
{
cin >> arr[i];
}
Merge_Sort(arr , 0 , size-1);
cout << "Array after applying Merge Sort: " << endl;
for(int i = 0 ; i < size ; i++)
{
cout << arr[i] << " ";
}
return 0;
}