标题:归并排序的实现...有个地方不理解...求助
取消只看楼主
CodeWays
Rank: 2
等 级:论坛游民
帖 子:62
专家分:61
注 册:2010-2-7
结帖率:66.67%
已结贴  问题点数:20 回复次数:0 
归并排序的实现...有个地方不理解...求助
程序代码:
#include <stdio.h>

#define LEN 8
int a[LEN] = { 5, 2, 4, 7, 1, 3, 2, 6 };

void merge(int start, int mid, int end)
{
    int n1 = mid - start + 1;
    int n2 = end - mid;
    int left[n1], right[n2];
    int i, j, k;

    for (i = 0; i < n1; i++)
        left[i] = a[start+i];
    for (j = 0; j < n2; j++)
        right[j] = a[mid+1+j];

    i = j = 0;
    k = start;
    while (i < n1 && j < n2)
        if (left[i] < right[j])
            a[k++] = left[i++];
        else
            a[k++] = right[j++];

    while (i < n1)
        a[k++] = left[i++];
    while (j < n2)
        a[k++] = right[j++];
}

void sort(int start, int end)
{
    int mid;
    if (start < end)
    {
        mid = (start + end) / 2;
        printf("sort  (%d-%d, %d-%d)    %d %d %d %d %d %d %d %d\n",
               start, mid, mid+1, end, a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7]);
        sort(start, mid);
        sort(mid+1, end);
        merge(start, mid, end);
        printf("merge (%d-%d, %d-%d) to %d %d %d %d %d %d %d %d\n",
               start, mid, mid+1, end, a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7]);
    }
}

int main(void)
{
    sort(0, LEN-1);

    return 0;
}

这是归并排序算法的实现,可是我不能够理解sort函数中的递归……能帮忙讲解一下吗?
搜索更多相关主题的帖子: start include 
2010-02-10 09:59



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-297522-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.014510 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved