-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMergeSort.cpp
58 lines (50 loc) · 1.46 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
#include<bits/stdc++.h>
using namespace std;
void PrintArray(int *array, int n) {
for (int i = 0; i < n; ++i)
printf("%d ", array[i]);
printf("\n");
}
void Merger(int arr[], int lo, int mi, int hi){
int *temp = new int[hi-lo+1];//temporary merger array
int i = lo, j = mi + 1;//i is for left-hand,j is for right-hand
int k = 0;//k is for the temporary array
while(i <= mi && j <=hi){
if(arr[i] <= arr[j])
temp[k++] = arr[i++];
else
temp[k++] = arr[j++];
}
//rest elements of left-half
while(i <= mi)
temp[k++] = arr[i++];
//rest elements of right-half
while(j <= hi)
temp[k++] = arr[j++];
//copy the mergered temporary array to the original array
for(k = 0, i = lo; i <= hi; ++i, ++k)
arr[i] = temp[k];
delete []temp;
}
void MergeSortHelper(int arr[], int lo, int hi){
int mid;
if(lo < hi){
mid = (lo + hi) >> 1;
MergeSortHelper(arr, lo, mid);
MergeSortHelper(arr, mid+1, hi);
Merger(arr, lo, mid, hi);
}
}
void MergeSort(int arr[], int arr_size){
MergeSortHelper(arr, 0, arr_size-1);
}
int main() {
int array[] = {94, 0, 50, 95, 333, 3, 54, 456, 1, 28};
int n = sizeof(array)/sizeof(array[0]);
printf("Before Merge Sort:\n");
PrintArray(array, n);
MergeSort(array, n);
printf("\nAfter Merge Sort:\n");
PrintArray(array, n);
return (0);
}