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

归并排序

时间:2015-04-07 00:45:36      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:c++   归并排序   

void merge(int srcarray[],int temparray[],int startindex,int midindex,int endindex)
{
    int i = startindex,j = midindex + 1,k = startindex;
    while (i <= midindex && j <= endindex)
    {
        if(srcarray[i] > srcarray[j])
        {
            temparray[k++] = srcarray[i++];
        }
        else
        {
            temparray[k++] = srcarray[j++];
        }
    }
    if(i <= midindex)
    {
        while(i <= midindex + 1)
            temparray[k++] = srcarray[i++];
    }
    else
    {
        while(j <= endindex + 1)
            temparray[k++] = srcarray[j++];
    }
    for(i = startindex;i <= endindex;i++)
    {
        srcarray[i] = temparray[i];
    }
}

void mergesort(int srcarray[],int temparray[],int startindex,int endindex)
{
    int midindex;
    if(startindex < endindex)
    {
        midindex = (startindex + endindex) / 2;
        mergesort(srcarray,temparray,startindex,midindex);
        mergesort(srcarray,temparray,midindex + 1,endindex);
        merge(srcarray,temparray,startindex,midindex,endindex);
    }
}

归并排序

标签:c++   归并排序   

原文地址:http://blog.csdn.net/llq108/article/details/44907847

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