ZJU PTA ds 6-1 Iterative Mergesort

Iterative Mergesort

How would you implement mergesort without using recursion?

The idea of iterative mergesort is to start from N sorted sublists of length 1, and each time to merge a pair of adjacent sublists until one sorted list is obtained. You are supposed to implement the key function of merging.

Format of functions:

void merge_pass( ElementType list[], ElementType sorted[], int N, int length );

The function merge_pass performs one pass of the merge sort that merges adjacent pairs of sublists from list into sorted. N is the number of elements in the list and length is the length of the sublists.

Sample program of judge:

#include <stdio.h>

#define ElementType int
#define MAXN 100

void merge_pass( ElementType list[], ElementType sorted[], int N, int length );

void output( ElementType list[], int N )
{
    int i;
    for (i=0; i<N; i++) printf("%d ", list[i]);
    printf("\n");
}

void  merge_sort( ElementType list[],  int N )
{
    ElementType extra[MAXN];  /* the extra space required */
    int  length = 1;  /* current length of sublist being merged */
    while( length < N ) { 
        merge_pass( list, extra, N, length ); /* merge list into extra */
        output( extra, N );
        length *= 2;
        merge_pass( extra, list, N, length ); /* merge extra back to list */
        output( list, N );
        length *= 2;
    }
} 


int main()
{
    int N, i;
    ElementType A[MAXN];

    scanf("%d", &N);
    for (i=0; i<N; i++) scanf("%d", &A[i]);
    merge_sort(A, N);
    output(A, N);

    return 0;
}

/* Your function will be put here */

Sample Input:

10
8 7 9 2 3 5 1 6 4 0

Sample Output:

7 8 2 9 3 5 1 6 0 4 
2 7 8 9 1 3 5 6 0 4 
1 2 3 5 6 7 8 9 0 4 
0 1 2 3 4 5 6 7 8 9 
0 1 2 3 4 5 6 7 8 9 

My code

/* Your function will be put here */
/*The function merge_pass performs one pass of the merge sort that merges adjacent pairs of sublists from list into sorted. N is the number of elements in the list and length is the length of the sublists.*/

void Merge(int A[], int tmpArray[], int lpos, int rpos, int rightEnd);

void merge_pass( ElementType list[], ElementType sorted[], int N, int length )
{
    int i;

    for (i = 0; i + 2 * length - 1 < N; i += 2 * length) {
        Merge(list, sorted, i, i + length, i + 2 * length - 1);
    }
    
    if (i == 0) {
        Merge(list, sorted, 0, length, N - 1);
    }
    else if (N - i > length) {
        Merge(list, sorted, i, i + length, N - 1);
    }
    else if (i < N){
        while (i < N) {
            sorted[i] = list[i];
            i++;
        }
    }
    
}

void Merge(int A[], int tmpArray[], int lpos, int rpos, int rightEnd)
{
    int i, leftEnd, num, tmpPos;
    leftEnd = rpos - 1;
    tmpPos = lpos;
    num = rightEnd - lpos + 1;

    while (lpos <= leftEnd && rpos <= rightEnd) {
        if (A[lpos] <= A[rpos]) {
            tmpArray[tmpPos++] = A[lpos++]; 
        }
        else {
            tmpArray[tmpPos++] = A[rpos++]; 
        }
    }
    while (lpos <= leftEnd) {
        tmpArray[tmpPos++] = A[lpos++]; 
    }
    while (rpos <= rightEnd) {
        tmpArray[tmpPos++] = A[rpos++]; 
    }

}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值