-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathMergeSort.cpp
60 lines (51 loc) · 1.45 KB
/
MergeSort.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
#include<iostream>
using namespace std;
void merge(int arr[], int lowLeft, int highLeft, int highRight){
int lowRight = highLeft + 1;
int size = highRight - lowLeft + 1;
int iRight, iLeft, iRes;
int *mergedArr; mergedArr = new int[size];
iLeft = lowLeft; iRight = lowRight;
iRes = 0;
while(iLeft<=highLeft && iRight <= highRight){
if(arr[iLeft]< arr[iRight]){
mergedArr[iRes] = arr[iLeft];
iLeft++; iRes++; }
else{
mergedArr[iRes] = arr[iRight];
iRight++; iRes++; }
}
while(iLeft<=highLeft){
mergedArr[iRes] = arr[iLeft];
iLeft++; iRes++;
}
while(iRight <= highRight){
mergedArr[iRes] = arr[iRight];
iRight++; iRes++;
}
for(int i = 0, iArr = lowLeft; i < size; i++, iArr++){
arr[iArr] = mergedArr[i];
}
delete []mergedArr;
}
void mergeSort(int arr[], int low, int high){
int mid;
if(low == high){
return;
}
else{
mid = (low+high)/2;
mergeSort(arr, low, mid);
mergeSort(arr, mid+1, high);
merge(arr, low, mid, high);
}
}
int main(){
int arr[9] = {14, 5, 8, 13, 1, 18, 3, -6};
int arrSize = 9;
mergeSort(arr, 0, arrSize-1);
for(int i: arr){
cout<<i<<" ";
}
return 0;
}