-
Notifications
You must be signed in to change notification settings - Fork 1
/
Merge Sort Using Array.cpp
78 lines (62 loc) · 1.61 KB
/
Merge Sort Using Array.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
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <iostream>
using namespace std;
// Merge two subarrays ar1 and ar2 into arr
void merge(int arr[], int left, int mid, int right) {
int n1 = mid - left + 1;
int n2 = right - mid;
int ar1[n1], ar2[n2];
for (int i = 0; i < n1; i++)
ar1[i] = arr[left + i];
for (int j = 0; j < n2; j++)
ar2[j] = arr[mid + 1 + j];
// Maintain current index of sub-arrays and main array
int i, j, k;
i = 0;
j = 0;
k = left;
// Until we reach either end of either ar1 or ar2, pick larger among
// elements L and M and place them in the correct position at A[p..r]
while (i < n1 && j < n2) {
if (ar1[i] <= ar2[j]) {
arr[k] = ar1[i];
i++;
} else {
arr[k] = ar2[j];
j++;
}
k++;
}
// When we run out of elements in either ar1 or ar2,
// pick up the remaining elements and put in A[p..r]
while (i < n1) {
arr[k] = ar1[i];
i++;
k++;
}
while (j < n2) {
arr[k] = ar2[j];
j++;
k++;
}
}
// Divide the array into two subarrays, sort them and merge them
void mergeSort(int arr[], int l, int r) {
if (l < r) {
// m is the point where the array is divided into two subarrays
int m = l + (r - l) / 2;
mergeSort(arr, l, m);
mergeSort(arr, m + 1, r);
// Merge the sorted subarrays
merge(arr, l, m, r);
}
}
int main() {
int arr[] = {6, 5, 12, 10, 9, 1};
int size = sizeof(arr) / sizeof(arr[0]); // Size of array
mergeSort(arr, 0, size - 1);
cout << "Sorted array: \n";
for (int i=0; i<size; i++){
cout<<arr[i]<<" ";
}
return 0;
}