-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmergeSort.cpp
58 lines (48 loc) · 877 Bytes
/
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
#include <iostream>
using namespace std;
int mergeSort(int a[], int s, int l);
int[] merge(int a[], int s, int e, int m);
int mergeSort(int a[], int s, int l){
int mid = (l - s - 1)/2;
mergeSort(a, s, mid);
mergeSort(a, mid, l);
merge(a, s, l ,mid);
return 0;
}
int[] merge(int a[], int s, int e, int m){
int i = s;
int j = m;
int k = 0;
int mix[e-s];
while((i < m) && (j < e)){
if(a[i] < a[j]){
mix[k] = a[i];
i++;
k++;
}
else{
mix[k] = a[j];
j++;
k++;
}
}
while(i < m){
mix[k] = a[i];
i++;
k++;
}
while(j<e){
mix[k] = a[j];
j++;
k++;
}
return mix[];
}
int main(){
int arr[] = {2,5,1,0,7};
mergeSort(arr, 0, 5);
for(int i = 0; i < 5; i++){
cout << arr[i] << endl;
}
return 0;
}