1. 程式人生 > >Data Structures and Algorithms (English) - 6-15 Iterative Mergesort(25 分)

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

題目連結:點選開啟連結

 

題目大意:略。

 

解題思路:略。

 

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;
    }
}