• 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;
    }
     
    无欲速,无见小利。欲速,则不达;见小利,则大事不成。
  • 相关阅读:
    论云端智能相册应用的架构设计及应用
    《架构漫谈》阅读笔记02
    论软件体系架构之质量属性
    《架构漫谈》阅读笔记01
    tensorflow-gpu:Could not load dynamic library 'cusolver64_10.dll'; dlerror: cusolver64_10.dll not found
    tensorflow线性回归
    【阿里巴巴国际站】22届实习生招聘开始啦!
    vue-ref、js获取dom子元素
    echarts地图tooltip添加标注
    echarts 图表坐标axisLabel格式化文字
  • 原文地址:https://www.cnblogs.com/ch122633/p/9023552.html
Copyright © 2020-2023  润新知