1 #include<iostream>
 2 using namespace std;
 3 
 4 
 5 void Merge(int a[],int a1,int n1, int b[],int b1,int n2, int c[])
 6 {
 7     int i = a1;
 8     int j = b1;
 9     int k = a1;
10     while (i <= n1&&j <= n2)
11     {
12         if (a[i] <= b[j])
13         {
14             c[k] = a[i];
15             i++;
16         }
17         else
18         {
19             c[k] = b[j];
20             j++;
21         }
22         k++;
23     }
24     if (i > n1)
25         while (j <= n2)
26             c[k++] = b[j++];
27     else
28         while (i <= n1)
29             c[k++] = a[i++];
30 }
31 
32 
33 
34 void Mergesort(int a[],int p,int r,int c[])
35 {
36 
37     if (p < r)
38     {
39         int mid = (p + r) / 2;
40         Mergesort(a, p, mid,c);
41         Mergesort(a, mid + 1, r,c);
42         Merge(a, p, mid, a, mid + 1, r, c);
43         for (int i = p; i <= r; i++)
44             a[i] = c[i];
45     }
46 }
47 
48 
49 
50 void main()
51 {
52     int a[11];
53     int c[11];
54     for (int i = 1; i < 11; i++)
55         a[i] = 10 - i;
56     Mergesort(a, 1, 10, c);
57     for (int i = 1; i < 11; i++)
58         cout << a[i] << endl;
59 }

 

posted on 2017-02-28 12:39  郑哲  阅读(166)  评论(0编辑  收藏  举报