-
Notifications
You must be signed in to change notification settings - Fork 81
Expand file tree
/
Copy pathMerge Sort.cpp
More file actions
71 lines (66 loc) · 1.35 KB
/
Merge Sort.cpp
File metadata and controls
71 lines (66 loc) · 1.35 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
/*
Time Complexity : O(n * log(n)) Space Complexity : O(n)
where n is size of input array
*/
#include <iostream>
#include "solution.h"
using namespace std;
void merge(int input1[], int size1, int input2[], int size2, int output[])
{
int i = 0, j = 0, k = 0;
while (i < size1 && j < size2)
{
if (input1[i] < input2[j])
{
output[k] = input1[i]; k++;
i++;
}
else
{
output[k] = input2[j]; j++;
k++;
}
}
while (i < size1)
{
output[k] = input1[i]; k++;
i++;
}
while (j < size2)
{
output[k] = input2[j]; j++;
k++;
}
}
void mergeSort(int input[], int size)
{
if (size <= 1)
{
return;
}
int mid = size / 2;
int part1[500], part2[500];
int size1 = mid, size2 = size - mid; for (int i = 0; i < mid; i++)
{
part1[i] = input[i];
}
int k = 0;
for (int i = mid; i < size; i++)
{
part2[k] = input[i]; k++;
}
mergeSort(part1, size1); mergeSort(part2, size2);
merge(part1, size1, part2, size2, input);
}
//Driver Code
int main() {
int length;
cin >> length;
int* input = new int[length];
for(int i=0; i < length; i++)
cin >> input[i];
mergeSort(input, length);
for(int i = 0; i < length; i++) {
cout << input[i] << " ";
}
}