zoukankan      html  css  js  c++  java
  • 09-排序2 Insert or Merge(25 分)

    According to Wikipedia:

    Insertion sort iterates, consuming one input element each repetition, and growing a sorted output list. Each iteration, insertion sort removes one element from the input data, finds the location it belongs within the sorted list, and inserts it there. It repeats until no input elements remain.

    Merge sort works as follows: Divide the unsorted list into N sublists, each containing 1 element (a list of 1 element is considered sorted). Then repeatedly merge two adjacent sublists to produce new sorted sublists until there is only 1 sublist remaining.

    Now given the initial sequence of integers, together with a sequence which is a result of several iterations of some sorting method, can you tell which sorting method we are using?

    Input Specification:

    Each input file contains one test case. For each case, the first line gives a positive integer N (100). Then in the next line, N integers are given as the initial sequence. The last line contains the partially sorted sequence of the N numbers. It is assumed that the target sequence is always ascending. All the numbers in a line are separated by a space.

    Output Specification:

    For each test case, print in the first line either "Insertion Sort" or "Merge Sort" to indicate the method used to obtain the partial result. Then run this method for one more iteration and output in the second line the resuling sequence. It is guaranteed that the answer is unique for each test case. All the numbers in a line must be separated by a space, and there must be no extra space at the end of the line.

    Sample Input 1:

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

    Sample Output 1:

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

    Sample Input 2:

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

    Sample Output 2:

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

    我的答案
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    
    void PrintA(int A[], int N)
    {
        int i;
        for(i=0;i<N;i++) {
            if(i != N-1)
                printf("%d ", A[i]);
            else
                printf("%d
    ", A[i]);
        }
    }
    
    int IsSameArr(int A[], int C[], int N)
    {
        int i;
        for(i=0;i<N;i++)
            if(A[i] != C[i]) return 0;
        return 1;
    }
    
    void CopyArr(int A[], int B[], int N)
    {
        int i;
        for(i=0;i<N;i++)
            B[i] = A[i];
    }
    
    void Read(int A[], int B[], int N)
    {
        int i;
        for(i=0;i<N;i++) {
            if(i!=N-1)
                scanf("%d ", &A[i]);
            else 
                scanf("%d
    ", &A[i]);
        }
    
        for(i=0;i<N;i++) {
            if(i!=N-1)
                scanf("%d ", &B[i]);
            else 
                scanf("%d
    ", &B[i]);
        }
    }
    
    void InsertionSort(int A[], int B[], int N)
    {
        int P, i;
        int Tmp;
        int flag = 0;
    
        for(P=1;P<N;P++) {
            Tmp = A[P];
            for(i=P;i>0 && A[i-1]>Tmp;i--)
                A[i] = A[i-1];
            A[i] = Tmp;
            if(IsSameArr(A, B, N)) { 
                printf("Insertion Sort
    ");
                flag = 1;
                continue;
            }
            if(flag== 1) {
                flag = 0;
                PrintA(A, N);
            }
        }
    }
    
    void Merge(int A[], int TmpA[], int L, int R, int RightEnd)
    {
        int LeftEnd = R - 1;
        int Tmp = L;
        int NumElements = RightEnd - L + 1;
        int i;
        while(L <= LeftEnd && R <= RightEnd) {
            if(A[L] <= A[R]) TmpA[Tmp++] = A[L++];
            else             TmpA[Tmp++] = A[R++];
        }
        while(L <= LeftEnd)
            TmpA[Tmp++] = A[L++];
        while(R <= RightEnd)
            TmpA[Tmp++] = A[R++];
        for(i=0;i<NumElements;i++, RightEnd--)
            A[RightEnd] = TmpA[RightEnd];
    }
    
    void Merge_Pass(int A[], int TmpA[], int N, int length)
    {
        int i, j;
        for(i=0;i<N-2*length;i+=2*length)
            Merge(A, TmpA, i, i+length, i+2*length-1);
        if(i+length < N)
            Merge(A, TmpA, i, i+length, N-1);
        else
            for(j=i;j<N;j++)    TmpA[j] = A[j];
    
    }
    
    void Merge_Sort(int A[], int B[], int N)
    {
        int *TmpA;
        int length = 1;
        int flag = 0;
        int flag2 = 0;
        TmpA = malloc(sizeof(int)*N);
        if(TmpA != NULL) {
            while(length < N) {
                Merge_Pass(A, TmpA, N, length);
                length *= 2;
                if(IsSameArr(A, B, N)) { 
                    printf("Merge Sort
    ");
                    flag = 1;
                }
                if(flag2 == 1) {
                    flag2 = 0;
                    PrintA(A, N);
                }
    
                Merge_Pass(TmpA, A, N, length);
                length *= 2;
                if(IsSameArr(A, B, N)) { 
                    printf("Merge Sort
    ");
                    flag2 = 1;
                }
                if(flag == 1) {
                    flag = 0;
                    PrintA(A, N);
                }
            }
            free(TmpA);
        } else
            printf("Sapce not enough");
    }
    
    int main()
    {
        int N;
        int *A, *B, *Tmp;
        scanf("%d
    ", &N);
        A = (int *)malloc(sizeof(int)*N);
        B = (int *)malloc(sizeof(int)*N);
        Tmp = (int *)malloc(sizeof(int)*N);
        Read(A, B, N);
        CopyArr(A, Tmp, N);
        InsertionSort(Tmp, B, N);
        CopyArr(A, Tmp, N);
        Merge_Sort(A, B, N);
        return 0;
    }
     
    无欲速,无见小利。欲速,则不达;见小利,则大事不成。
  • 相关阅读:
    【160406 24:00】四则运算 4(结对开发 2)
    【160319 24:00】四则运算 3(结对开发 1)
    【160313 18:00】四则运算 2 的单元测试
    【160312 18:00】四则运算 2
    【Web前端Talk】React-loadable 进行代码分割的基本使用
    【Web前端Talk】无聊吗?写个【飞机大战】来玩吧(下篇)
    【Web前端Talk】“用数据说话,从埋点开始”-带你理解前端的三种埋点
    【Web前端Talk】无聊吗?写个【飞机大战】来玩吧(上篇)
    windows环境下搭建vue开发环境
    Sublime Text3添加css兼容前缀插件
  • 原文地址:https://www.cnblogs.com/ch122633/p/9023552.html
Copyright © 2011-2022 走看看