-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMergeSort.cpp
More file actions
49 lines (41 loc) · 876 Bytes
/
Copy pathMergeSort.cpp
File metadata and controls
49 lines (41 loc) · 876 Bytes
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
#include<iostream>
using namespace std;
void merge(int *a, int start, int end, int *t) {
if (start >= end)
return;
int mid = (start + end) / 2;
int start1 = start;
int start2 = mid + 1;
int end1 = mid;
int end2 = end;
merge(a, start1, end1, t);
merge(a, start2, end2, t);
int k = start;
while (start1 <= end1 && start2 <= end2) {
t[k++] = a[start1] < a[start2] ? a[start1++] : a[start2++];
}
while (start1 <= end1)
t[k++] = a[start1++];
while (start2 <= end2)
t[k++] = a[start2++];
for (int i = start; i <=end; i++) {
a[i] = t[i];
cout << a[i]<<" ";
}
cout << endl;
}
void mergeSort(int* a, int len) {
if (len == 0)
return;
int *t = new int[len];
merge(a, 0, len - 1,t);
delete[] t;
}
int main() {
int a[10] = { 9,8,7,6,5,4,1,2,3,10 };
mergeSort(a, 10);
for (int i = 0; i < 10; i++) {
cout << a[i] << " " ;
}
system("pause");
}