forked from mrizky-kur/Redux-toolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mergesort.cpp
64 lines (49 loc) · 1.25 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
62
63
64
#include<iostream>
int A[20], P, Q, R, n1,n2, LA[10],RA[10];
void merge_sort(int A[], int P, int R);
void merge(int A[], int P, int Q, int R);
int main(void) {
int size;
std::cout << "Enter the size of array: ";
std::cin >> size;
std::cout << "\nEnter the elements of array: ";
for(int i = 0; i < size; i++)
std::cin >> A[i];
P = 0, R =size - 1;
merge_sort(A, P, R);
std::cout << "\nPrinting the sorted array: ";
for(int i = 0; i < 10; i++)
std::cout << A[i] << " ";
return 0;
}
void merge_sort(int A[], int P, int R) {
int Q;
if(P < R) { //if start < end
Q = (P + R)/2; //mid =(start + end) / 2
merge_sort(A, P, Q);
merge_sort(A, Q + 1, R);
merge(A, P, Q, R);
}
}
void merge(int A[], int P, int Q, int R) {
n1 = Q - P + 1; //mid - start + 1
n2 = R - Q; //end - mid
int i, j, k;
for(i =1; i<= n1; i++)
LA[i] = A[P + i - 1];
for(j = 1; j <= n2; j++)
RA[j] = A[Q + j];
i = 1, j =1;
LA[n1 + 1] = 32767;
RA[n2 + 1] = 32767;
for(k = P; k <= R; k++) {
if(LA[i] <= RA[j] ) {
A[k] = LA[i];
i++;
}
else {
A[k] = RA[j];
j++;
}
}
}