码迷,mamicode.com
首页 > 其他好文 > 详细

归并排序

时间:2014-07-02 09:13:46      阅读:213      评论:0      收藏:0      [点我收藏+]

标签:归并排序

void merge_array(int list1,int list1_size,int list2,int list2_size)
{
    int i,j,k;
    i=j=k=0;
    
    //声明临时数组用暂存归并结果
    int list[list1_size + list2_size];
    
    while(i < list1_size && j < list2_size)
        list[k++] = list1[i] <= list2[j] ? list1[i++] : list2[j++];
    while(i < list1_size)
        list[k++] = list1[i++];
    while(j < list2_size)
        list[k++] = list2[j++];
    for(int ii = 0;ii < list1_size + list2_size;++ii)
        list1[ii] = list[ii];
}   

void merge_sort(int *list,int list_size)
{
    if(list_size > 1)
    {
        //把数组平均分成两份
        int *list1 = list;
        int list1_size = list_size / 2;
        int *list2 = list + list_size / 2;
        int list2_size = list_size - list1_size;
<pre name="code" class="html">       <span style="white-space:pre">	</span>merge_sort(list1, list1_size);
        merge_sort(list2,list2_size);

<span style="white-space:pre">	</span>
        merge_array(list1,list1_size,list2,list2_size);
    }
}
 
        

归并排序,布布扣,bubuko.com

归并排序

标签:归并排序

原文地址:http://blog.csdn.net/u012118523/article/details/36218247

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