归并操作的过程如下:
|
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;int data[8]={1,2,3,4,1,9,6,8};int merge(int
unSorted[],int
sorted[],int
first,int
mid,int
last){ int
fpoint=first; int
spoint=mid+1; int
sortedPoint=first; while(fpoint<=mid && spoint<=last){ if(unSorted[fpoint]<unSorted[spoint]) sorted[sortedPoint++]=unSorted[fpoint++]; else sorted[sortedPoint++]=unSorted[spoint++]; } if(fpoint>mid) while(sortedPoint<=last) sorted[sortedPoint++]=unSorted[spoint++]; if(spoint>last) while(sortedPoint<=last) sorted[sortedPoint++]=unSorted[fpoint++]; for(int
i=first;i<=last;i++) unSorted[i]=sorted[i]; return
0;}int
myMergeSort(int
unSorted[],int
sorted[],int
begin,int
end){ if(begin<end){ int
mid=(begin+end)/2; myMergeSort(unSorted,sorted,begin,mid); myMergeSort(unSorted,sorted,mid+1,end); merge(unSorted,sorted,begin,mid,end); } return
0;}int
main(){ int
*sortedData=(int
*)malloc(sizeof(data)); //merge(data,sortedData,0,3,7); myMergeSort(data,sortedData,0,7); for(int
i=0;i<8;i++) cout<<sortedData[i]<<" "; getchar(); return
0;} |
归并排序(Merge Sort),布布扣,bubuko.com
原文:http://www.cnblogs.com/seair/p/3629304.html