forked from a-r-nida/HactoberFest2020-Beginers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
merge-sort.cpp
65 lines (58 loc) · 1.31 KB
/
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 <iostream>
using namespace std;
void print(int a[], int sz)
{
for (int i = 0; i < sz; i++) cout << a[i] << " ";
cout << endl;
}
void merge(int a[], const int low, const int mid, const int high)
{
int *temp = new int[high-low+1];
int left = low;
int right = mid+1;
int current = 0;
while(left <= mid && right <= high) {
if(a[left] <= a[right]) {
temp[current] = a[left];
left++;
}
else {
temp[current] = a[right];
right++;
}
current++;
}
if(left > mid) {
for (int i = right; i <= high; i++) {
temp[current] = a[i];
current++;
}
}
else {
for(int i=left; i <= mid; i++) {
temp[current] = a[i];
current++;
}
}
for(int i=0; i<=high-low;i++) {
a[i+low] = temp[i];
}
delete[] temp;
}
void merge_sort(int a[], const int low, const int high)
{
if(low >= high) return;
int mid = (low+high)/2;
merge_sort(a, low, mid);
merge_sort(a, mid+1, high);
merge(a, low, mid, high);
}
int main()
{
int a[] = {38, 27, 43, 3, 9, 82, 10};
int arraySize = sizeof(a)/sizeof(int);
print(a, arraySize);
merge_sort(a, 0, (arraySize-1) );
print(a, arraySize);
return 0;
}