-
Notifications
You must be signed in to change notification settings - Fork 3
/
mgsort.cpp
68 lines (63 loc) · 1.26 KB
/
mgsort.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
59
60
61
62
63
64
65
66
67
68
#include <iostream>
#include <string.h>
using namespace std;
void merge(int number[], int start, int middle, int end)
{
if(start == end)
{
return;
}
int len = end - start + 1;
int * newNum = new int [len];
int i = 0;
int left = start, right = middle + 1;
while(left <= middle && right <= end)
{
if(number[left] < number[right])
{
newNum[i++] = number[left++];
}
else
{
newNum[i++] = number[right++];
}
}
if(left <= middle)
{
memcpy(&newNum[i], &number[left], (middle - left + 1)* sizeof(int));
}
else if(right <= end)
{
memcpy(&newNum[i], &number[right], (end - right + 1) * sizeof(int));
}
memcpy(number+start, newNum, len * sizeof(int));
delete [] newNum;
return;
}
void mgsort(int number[], int start, int end)
{
if(start >= end)
{
return;
}
int middle = start + (end - start)/2;
if(start <= middle)
{
mgsort(number, start, middle);
}
if(middle < end)
{
mgsort(number, middle+1, end);
}
merge(number, start, middle, end);
return;
}
int main(void)
{
int array[10] = {1,8, 5, 3, 8,2,7,4,6, 10};
mgsort(array, 0, 9);
for(int i = 0; i < 10; i++)
{
cout << array[i] << " ";
}
}