-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
79 lines (70 loc) · 1.87 KB
/
Copy pathmain.cpp
File metadata and controls
79 lines (70 loc) · 1.87 KB
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
#include <iostream>
using namespace std;
// Time complexity: O(nlogn), Space complexity: O(n)
// This function will merge two sorted arrays
void mergeArrays(int arr[], int start, int end)
{
int size = end - start + 1;
int middle = (start + end)/2;
int * output = new int[size];
int i = start; // counter 1
int j = middle + 1; // counter 2
int k = 0; // counter for the output array
while (i <= middle && j <= end)
{
if (arr[i] < arr[j])
{
output[k] = arr[i];
i++; // move on to the next element of the first half
}
else
{
output[k] = arr[j]; // move on to the next element of the second half
j++;
}
k++; // increase the counter of the output array
}
// Copy the elements if there are some present in the first half
while (i <= middle)
{
output[k] = arr[i];
i++;
k++;
}
// Copy the rest of the elements from the second half if they are present
while (j <= end)
{
output[k] = arr[j];
j++;
k++;
}
// Copy the output array into the input array
int m = 0;
for (int i = start; i <= end; i++) {
arr[i] = output[m];
m++;
}
delete []output; // delete the output array
}
void mergeSort(int input[], int start, int end)
{
if(start >= end)
{
return;
}
int middle = (end + start)/2;
mergeSort(input, start, middle); // first half
mergeSort(input, middle + 1, end); // second half
mergeArrays(input, start, end); // now we will merge these above half sorted arrays into one
}
int main()
{
int input[] = {8, 10, 2, 0, 90, 22};
int size = sizeof(input)/ sizeof(int);
mergeSort(input, 0, size - 1);
for (int i = 0; i < size; i++)
{
cout << input[i] << " ";
}
cout << endl;
}