Data Structures and Algorithms (English) - 6-15 Iterative Mergesort(25 分)

版权声明:本文为博主原创文章,转载前动动小手点个【赞】吧~ https://blog.csdn.net/Dream_Weave/article/details/83583260

题目链接:点击打开链接

题目大意:略。

解题思路:略。

AC 代码

void merge_pass( ElementType list[], ElementType sorted[], int N, int length )
{
    int idx=0, p1=0, len1=p1+length, p2=len1, len2=p2+length;
    int first=0;
    while(1)
    {
        if(len2>N)
        {
            len2=N;
            first=1; // 最后一组
        }

        while(p1<len1&&p2<len2)
        {
            if(list[p1]<list[p2]) sorted[idx++]=list[p1++];
            else sorted[idx++]=list[p2++];
        }

        while(p1<len1) sorted[idx++]=list[p1++];
        while(p2<len2) sorted[idx++]=list[p2++];

        p1=len2, len1=p1+length;
        p2=len1, len2=p2+length;

        if(first) return;
    }
}

猜你喜欢

转载自blog.csdn.net/Dream_Weave/article/details/83583260