-
Notifications
You must be signed in to change notification settings - Fork 2
/
mergeSort.cpp
61 lines (60 loc) · 1.48 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
61
//TO-DO Refactor the code using vector instead of arrays.
#include<iostream>
#include<vector>
using namespace std;
class mergeSortClass{
public:
void mergeSort(int *arr, int low, int high){
int mid;
if(low < high){
mid = (low+high)/2;
mergeSort(arr,low,mid);
mergeSort(arr,mid+1,high);
merge(arr,low,high,mid);
}
}
void merge(int *arr, int low, int high, int mid){
int sortedArray[50];
int i = low;
int k = low;
int j = mid+1;
while(i<=mid && j<=high){
if(arr[i]<arr[j]){
sortedArray[k] = arr[i];
i++;
k++;
}
else{
sortedArray[k] = arr[j];
j++;
k++;
}
}
while(i<=mid){
sortedArray[k] = arr[i];
i++;
k++;
}
while(j<=high){
sortedArray[k] = arr[j];
j++;
k++;
}
for(i=low;i<k;i++){
arr[i] = sortedArray[i];
}
}
static void printVector(int *inputVector,int size){
for(int i=0;i<size;i++){
cout<<inputVector[i]<<" ";
}
cout<<endl;
}
};
int main(){
int myarray[10] = {1,3,2,10,8,9,4,6,5,7};
int size = sizeof(myarray)/sizeof(myarray[0]);
mergeSortClass firstSort;
firstSort.mergeSort(myarray, 0, size-1);
firstSort.printVector(myarray,size);
}