-
Notifications
You must be signed in to change notification settings - Fork 0
/
MergeSort.cpp
51 lines (41 loc) · 1.34 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
void mergeArray(vector<int>& array, const int first, const int mid, const int last){
const int sizeA = mid - first + 1;
const int sizeB = last - mid;
int indexC = first;
// create two temp arrays
int* arrA = new int[sizeA];
int* arrB = new int[sizeB];
for(int i = 0 ; i < sizeA ; i++)
arrA[i] = array[first + i];
for(int i = 0 ; i < sizeB ; i++)
arrB[i] = array[mid + 1 + i];
int indexA, indexB;
indexA = indexB = 0;
//Merge array[arrA .. arrB]
while(indexA < sizeA && indexB < sizeB){
if(arrA[indexA] <= arrB[indexB] || indexB == sizeB)
array[indexC++] = arrA[indexA++];
else
array[indexC++] = arrB[indexB++];
}
//add the remaining elements of arrA and arrB
while(indexA < sizeA)
array[indexC++] = arrA[indexA++];
while(indexB < sizeB)
array[indexC++] = arrB[indexB++];
delete[] arrA;
delete[] arrB;
}
//Divides array into 2 {Recursive}
void mergeSortX(vector<int>& array, const int first, const int last){
if(first >= last)
return;
int mid = first + (last - first)/2;
mergeSortX(array, first, mid);
mergeSortX(array, mid + 1, last);
mergeArray(array, first, mid, last);
}
void mergeSort(vector < int > & arr, int n) {
if(n == 0 || n == 1) return;
mergeSortX(arr, 0, n-1);
}