-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMergeSort.cpp
86 lines (61 loc) · 1.75 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <iostream>
void Merge(int array[], int p, int left, int right);// Merge Function
void Merge_Sort(int array[], int left, int right); //actual merge sort
int main() {
//input, unsorted array>
int arr[9]{9,-5,-2,40,41,5,6,5,19};
//merge sort the array of boundaries 0 ->8 inclusive
Merge_Sort(arr, 0, 8);
//printing the array
for(auto x : arr)
std::cout<<x<<std::endl;
}
void Merge_Sort(int array[], int left, int right)
{
//if the array is empty or of one element, we're done.
if(left<right)
{
//speicify mid point p to separate left & right
int p = (left+right)/2;
//merge sort the left -> mid point
Merge_Sort(array, left,p);
//merge sort mid + 1 -> right
Merge_Sort(array,p+1,right);
//merge both the left and the right.
Merge(array,p,left,right);
}
}
void Merge(int array[], int p,int left, int right)
{
// initialise new arrays to have the left and right side.
// we add an extra sentile element to each array (infinity) at line 64 65.
int L[p - left + 1 + 1];
int R[right-p+1];
//setting the boundaries for each new array (L and R)
int n1 = p-left;
int n2 = right - p;
//getting elemetns from oringal array to each array
for(int i =0;i<=n1;i++)
{ L[i] = array[left+i]; }
//same here.
for(int j =0; j<n2 ;j++)
{ R[j]= array[p +1 + j]; }
L[n1+1] = 1000000; //the extra sentile element at the end
R[n2] = 1000000;
//Merging the 2 arrays
int i =0; int j = 0;
for (int k = left;k<right+1;k++)
{
if (L[i]< R[j])
{
array[k] = L[i];
++i;
}
else
{
array[k] = R[j];
++j;
}
// because we add an extra element at the end, we don't need to check if the L, R arrays are empty.
}
}