zoukankan      html  css  js  c++  java
  • 6-15 Iterative Mergesort (25分)

    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 sortedN 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("
    ");
    }
    
    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 

    代码:
    void merge_pass( ElementType list[], ElementType sorted[], int N, int length ) {
        for(int i = 0;i < N;i += length * 2) {
            int a = i,b = i + length,j = 0;
            int aa = i + length,bb = i + length * 2;
            if(aa > N) aa = N;
            if(bb > N) bb = N;
            while(a < aa && b < bb) {
                if(list[a] < list[b]) {
                    sorted[i + j ++] = list[a ++];
                }
                else sorted[i + j ++] = list[b ++];
            }
            while(a < aa) {
                sorted[i + j ++] = list[a ++];
            }
            while(b < bb) {
                sorted[i + j ++] = list[b ++];
            }
        }
    }
  • 相关阅读:
    JAVA第三周学习总结
    20175303 2018-2019-2 《Java程序设计》第2周学习总结
    20175310 MyCP(课下作业,必做)
    20175310 《Java程序设计》第8周学习总结
    2018-2019-2 20175310 实验二《Java面向对象程序设计》实验报告
    20175310 《Java程序设计》第7周学习总结
    结对编程项目-四则运算 整体总结博客
    20175310 《Java程序设计》第6周学习总结
    20175310结对编程项目-四则运算 阶段总结博客
    20175310 类定义
  • 原文地址:https://www.cnblogs.com/8023spz/p/12255180.html
Copyright © 2011-2022 走看看