Chinaunix首页 | 论坛 | 博客
  • 博客访问: 695187
  • 博文数量: 90
  • 博客积分: 3225
  • 博客等级: 少校
  • 技术积分: 1200
  • 用 户 组: 普通用户
  • 注册时间: 2010-08-20 11:04
个人简介

菩提本无树,明镜变非台 本来无一物,何处惹尘埃

文章分类

全部博文(90)

文章存档

2015年(1)

2014年(12)

2013年(15)

2012年(31)

2011年(8)

2010年(23)

分类: C/C++

2010-12-03 15:11:09

 归并排序,它采取分而治之的策略,将两个已经排序的序列合并成一个序列的操作。
 时间复杂度是Θ(nlgn),优于插入排序算法。

 算法描述
    1) 申请空间,使其大小为两个已经排序序列之和,该空间用来存放合并后的序列
    2) 设定两个指针,最初位置分别为两个已经排序序列的起始位置
    3) 比较两个指针所指向的元素,选择相对小的元素放入到合并空间,并移动指针到下一位置
    4) 重复步骤3直到某一指针达到序列尾
    5) 将另一序列剩下的所有元素直接复制到合并序列尾
  特点:归并排序是稳定的排序.即相等的元素的顺序不会改变,  速度仅次于快速排序,但较稳定。

 示例代码      merge_sort.rar  

点击(此处)折叠或打开

  1. void merge(int *array,const int first, const int mid, const int last)
  2. {
  3.     int i,index;
  4.     int first1,last1;
  5.     int first2,last2;
  6.     int *tmp;
  7.     tmp = (int *)malloc((last-first+1)*sizeof(int));
  8.     if( tmp == NULL )
  9.         return;
  10.     first1 = first;
  11.     last1 = mid;
  12.     first2 = mid+1;
  13.     last2 = last;
  14.     index = 0;
  15.     while( (first1 <= last1) && (first2 <= last2) )
  16.     {
  17.         if( array[first1] < array[first2] )
  18.         {
  19.             tmp[index++] = array[first1];
  20.             first1++;
  21.         }
  22.         else{
  23.             tmp[index++] = array[first2];
  24.             first2++;
  25.         }
  26.     }
  27.     while( first1 <= last1 )
  28.     {
  29.         tmp[index++] = array[first1++];
  30.     }
  31.     while( first2 <= last2 )
  32.     {
  33.         tmp[index++] = array[first2++];
  34.     }
  35.     for( i=0; i<(last-first+1); i++)
  36.     {
  37.         array[first+i] = tmp[i];
  38.     }
  39.     free(tmp);
  40. }
  41. /*** 使用递归实现 ***/
  42. void merge_sort(int *array, const int first, const int last)
  43. {
  44.     int mid=0;
  45.     if(first < last)
  46.     {
  47.         mid=(first+last)/2;
  48.         merge_sort(array,first,mid);
  49.         merge_sort(array,mid+1,last);
  50.         merge(array,first,mid,last);
  51.         display_array(first, last-first+1, array+first);
  52.     }
  53. }

  54. /*** 使用迭代实现 ***/
  55. /*
  56. void merge_sort(int *list, const int first, const int last)
  57. {
  58.     int len= last-first+1;
  59.     int left_min,left_max;
  60.     int right_min,right_max;
  61.     int index;
  62.     int i;
  63.     int *tmp;
  64.     tmp = (int *)malloc(sizeof(int)*len);
  65.     if( tmp == NULL || len <= 0 )
  66.         return;
  67.     
  68.     for( i = 1; i < len; i *= 2 )
  69.     {
  70.         for( left_min = 0; left_min < len - i; left_min = right_max)
  71.         {
  72.             int j;
  73.             right_min = left_max = left_min + i;
  74.             right_max = left_max + i;
  75.             j = left_min;
  76.             if ( right_max > len )
  77.                 right_max = len;
  78.             index = 0;
  79.             while( left_min < left_max && right_min < right_max )
  80.             {
  81.                 tmp[index++] = (list[left_min] > list[right_min] ? list[right_min++] : list[left_min++]);
  82.             }
  83.             while( left_min < left_max )
  84.             {
  85.                 list[--right_min] = list[--left_max];
  86.             }
  87.             while( index > 0 )
  88.             {
  89.                 list[--right_min] = tmp[--index];
  90.             }
  91.             display_array(j,i*2,list+j);
  92.         }
  93.     }
  94.     free(tmp);
  95. }
  96. */
执行结果
  使用递归执行的结果


  使用迭代执行的结果

阅读(12374) | 评论(1) | 转发(1) |
1

上一篇:C 插入排序

下一篇:linux的设备模型

给主人留下些什么吧!~~

yaogongzuo2014-05-24 11:36:59

自己用C++编了后,内存老泄露,在网上找了这么久就楼主的管用