码迷,mamicode.com
首页 > 编程语言 > 详细

排序算法--归并排序(merge)

时间:2015-06-16 16:20:20      阅读:153      评论:0      收藏:0      [点我收藏+]

标签:

 归并排序是利用递归和分而治之的技术将数据序列划分成为越来越小的半子表,再对半子表排序,最后再用递归步骤将排好序的半子表合并成为越来越大的有序序列,归并排序包括两个步骤,分别为:

      1)划分子表   2)合并半子表 

 时间复杂度是Θ(nlgn),优于插入排序算法。

 

     算法描述
    1) 申请空间,使其大小为两个已经排序序列之和,该空间用来存放合并后的序列
    2) 设定两个指针,最初位置分别为两个已经排序序列的起始位置
    3) 比较两个指针所指向的元素,选择相对小的元素放入到合并空间,并移动指针到下一位置
    4) 重复步骤3直到某一指针达到序列尾
    5) 将另一序列剩下的所有元素直接复制到合并序列尾
    特点:归并排序是稳定的排序.即相等的元素的顺序不会改变,  速度仅次于快速排序,但较稳定。
 
  算法实现:
 1 //merge-sort
 2 void merge(int *arry,const int first,const int mid,const int last)
 3 {
 4     int i,index;
 5     int first1,last1;
 6     int first2,last2;
 7     int *tmp;
 8     tmp=(int*)malloc((last-first+1)*sizeof(int));
 9     if (tmp==NULL)
10         return ;
11     first1=first;
12     last1=mid;
13     first2=mid+1;
14     last2=last;
15     index=0;
16     while((first1<=last1)&&(first2<=last2))
17     {
18         if (arry[first1]<arry[first2])
19         {
20             tmp[index++]=arry[first1];
21             first1++;
22         }
23         else{
24             tmp[index++]=arry[first2];
25             first2++;
26         }
27     }
28     while(first1<=last1)
29     {
30         tmp[index++]=arry[first1++];
31     }
32     while(first2<=last2)
33     {
34         tmp[index++]=arry[first2++];
35     }
36     for (i=0;i<last-first+1;i++)
37     {
38         arry[first+i]=tmp[i];
39     }
40     free(tmp);
41 }
42 
43 /*使用递归实现归并排序*/
44 void merge_sort(int *arry,const int first,const int last)
45 {
46     int mid=0;
47     if (first<last)
48     {
49         mid=(first+last)/2;
50         merge_sort(arry,first,mid);
51         merge_sort(arry,mid+1,last);
52         merge(arry,first,mid,last);
53     }
54 }

另附:实现方式使用迭代方式实现(个人感觉递归要代码好看一些)

 1 /*** 使用迭代实现 ***/
 2 void merge_sort(int *list, const int first, const int last)
 3 {
 4     int len= last-first+1;
 5     int left_min,left_max;
 6     int right_min,right_max;
 7     int index;
 8     int i;
 9     int *tmp;
10     tmp = (int *)malloc(sizeof(int)*len);
11     if( tmp == NULL || len <= 0 )
12         return;
13     
14     for( i = 1; i < len; i *= 2 )
15     {
16         for( left_min = 0; left_min < len - i; left_min = right_max)
17         {
18             int j;
19             right_min = left_max = left_min + i;
20             right_max = left_max + i;
21             j = left_min;
22             if ( right_max > len )
23                 right_max = len;
24             index = 0;
25             while( left_min < left_max && right_min < right_max )
26             {
27                 tmp[index++] = (list[left_min] > list[right_min] ? list[right_min++] : list[left_min++]);
28             }
29             while( left_min < left_max )
30             {
31                 list[--right_min] = list[--left_max];
32             }
33             while( index > 0 )
34             {
35                 list[--right_min] = tmp[--index];
36             }
37             display_array(j,i*2,list+j);
38         }
39     }
40     free(tmp);
41 }

测试调用实例

 1 int main()
 2 {
 3     int arry[]={1,2,5,6,4,7};
 4     merge_sort(arry,0,5);
 5     for (int i=0;i<6;i++)
 6     {
 7         cout<<arry[i]<<" ";
 8     }
 9     system("pause");
10     return 0;
11 }

博客参考:http://blog.chinaunix.net/uid-24467128-id-2606195.html

     http://www.cnblogs.com/jillzhang/archive/2007/09/16/894936.html

排序算法--归并排序(merge)

标签:

原文地址:http://www.cnblogs.com/jingliming/p/4580980.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!