Chinaunix首页 | 论坛 | 博客
  • 博客访问: 101129
  • 博文数量: 20
  • 博客积分: 648
  • 博客等级: 上士
  • 技术积分: 222
  • 用 户 组: 普通用户
  • 注册时间: 2010-10-02 11:43
文章分类

全部博文(20)

文章存档

2013年(3)

2012年(8)

2011年(7)

2010年(2)

我的朋友

分类: C/C++

2011-03-25 13:32:21

归并排序:

分为三步:

1.Divide Step(拆分)

2. Conquer Step(递归)

3.Combine Step(合并)

对数据a[p,r]进行排序:

MERGE-SORT (A, p, r)

1.     IF p < r                                                     // Check for base case
2.         THEN q = FLOOR[(p + r)/2]                 // Divide step
3.                 MERGE-SORT (A, p, q)                 // Conquer step.
4.                 MERGE-SORT (A, q + 1, r)           // Conquer step.
5.                 MERGE (A, p, q, r)                       // Combine step.

MERGE (A, p, q, r )

1.      n1 ← qp + 1
2.      n2 ← rq
3.      Create arrays L[1 . . n1 + 1] and R[1 . . n2 + 1]
4.      FOR i ← 1 TO n1
5.            DO L[i] ← A[p + i − 1]
6.      FOR j ← 1 TO n2
7.            DO R[j] ← A[q + j ]
8.      L[n1 + 1] ← ∞
9.      R[n2 + 1] ← ∞
10.    i ← 1
11.    j ← 1
12.    FOR kp TO r
13.         DO IF L[i ] ≤ R[ j]
14.                THEN A[k] ← L[i]
15.                        ii + 1
16.                ELSE A[k] ← R[j]
17.                        jj + 1

完整代码:


  1. void mergeSort(int numbers[], int temp[], int array_size)

  2. {
  3.         m_sort(numbers, temp, 0, array_size - 1);

  4. }



  5. void m_sort(int numbers[], int temp[], int left, int right)

  6. {
  7.         int mid;

  8.         if (right > left)

  9.         {

  10.             mid = (right + left) / 2;

  11.             m_sort(numbers, temp, left, mid);

  12.             m_sort(numbers, temp, mid+1, right);


  13.             merge(numbers, temp, left, mid+1, right);

  14.         }

  15. }



  16. void merge(int numbers[], int temp[], int left, int mid, int right)

  17.         {

  18.             int i, left_end, num_elements, tmp_pos;


  19.             left_end = mid - 1;

  20.             tmp_pos = left;

  21.             num_elements = right - left + 1;



  22. while ((left <= left_end) && (mid <= right))

  23.         {

  24.                 if (numbers[left] <= numbers[mid])

  25.                 {

  26.                         temp[tmp_pos] = numbers[left];

  27.                         tmp_pos = tmp_pos + 1;

  28.                         left = left +1;

  29.                 }

  30.                 else

  31.                 {

  32.                         temp[tmp_pos] = numbers[mid];

  33.                         tmp_pos = tmp_pos + 1;

  34.                         mid = mid + 1;

  35.                 }

  36.         }



  37.         while (left <= left_end)

  38.                 {

  39.                         temp[tmp_pos] = numbers[left];

  40.                         left = left + 1;

  41.                         tmp_pos = tmp_pos + 1;

  42.                 }

  43.                 while (mid <= right)

  44.                 {

  45.                         temp[tmp_pos] = numbers[mid];

  46.                         mid = mid + 1;

  47.                         tmp_pos = tmp_pos + 1;

  48.                 }



  49.                 for (i = 0; i <= num_elements; i++)

  50.                 {

  51.                         numbers[right] = temp[right];

  52.                         right = right - 1;

  53.                 }

  54.         }
阅读(955) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~