forked from oviiii-m/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmergeSort.cpp
More file actions
54 lines (50 loc) · 1.02 KB
/
mergeSort.cpp
File metadata and controls
54 lines (50 loc) · 1.02 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
#include <iostream>
using namespace std;
void helper(int input[], int si, int mid, int ei)
{
int *temp = new int[ei-si+1];
int i=si, j=mid+1, k=0;
while(i <= mid && j <= ei){
if(input[i] < input[j]){
temp[k++] = input[i++];
}
else{
temp[k++] = input[j++];
}
}
while(i <= mid){
temp[k++] = input[i++];
}
while(j <= ei){
temp[k++] = input[j++];
}
k = 0;
for(int i=si; i<=ei; i++){
input[i] = temp[k++];
}
return;
}
void merge(int input[], int si, int ei)
{
if(si >= ei){
return;
}
int mid = (si+ei)/2;
merge(input, si, mid);
merge(input, mid+1, ei);
helper(input, si, mid, ei);
}
void mergeSort(int input[], int size){
merge(input, 0, size-1);
}
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] << " ";
}
}