• 09-排序3 Insertion or Heap Sort(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.

    Heap sort divides its input into a sorted and an unsorted region, and it iteratively shrinks the unsorted region by extracting the largest element and moving that to the sorted region. it involves the use of a heap data structure rather than a linear-time search to find the maximum.

    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 "Heap 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 6 0
    6 4 5 1 0 3 2 7 8 9
    

    Sample Output 2:

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

    我的答案
    #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]);
        }
    }
    
    int InsertionSort(int A[], int B[], int N)
    {
        int P, i;
        int Tmp, ret = 0;
        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;
                ret = 1;
                continue;
            }
            if(flag== 1) {
                flag = 0;
                PrintA(A, N);
            }
        }
        if(flag == 1)
            PrintA(A, N);
    
        return ret;
    }
    
    void PrecDown(int A[], int P, int N)
    {
        int Parent, Child, temp = A[P];
        for(Parent=P;Parent*2+1<N;Parent=Child) {
            Child = Parent*2+1;
    // printf("Child=%d A[Child]=%d temp=%d 
    ", Child, A[Child], temp);
            if((Child < N-1)&&(A[Child+1] > A[Child]))
                Child++;
            if(temp < A[Child]) 
                A[Parent] = A[Child];
            else
                break;
            
            // printf("[PecDown] ");
            // PrintA(A, N);
        }
        A[Parent] = temp;
    }
    
    void Swap(int *a, int *b)
    {
        int tmp;
        tmp = *b;
        *b = *a;
        *a = tmp;
    }
    
    void Heap_Sort(int A[], int B[], int N)
    {
        int i, flag = 0;
        // PrintA(A, N);
        for(i=N/2-1;i>=0;i--) {
            // printf("Heap_Sort[i] = %d
    ", i);
            PrecDown(A, i, N);
            if(IsSameArr(A, B, N)) {
                printf("Heap Sort
    ");
                flag = 1;
                continue;
            }
            if(flag == 1) {
                flag = 0;
                PrintA(A, N);
            }
        }
    
    // printf("-----------
    ");
    
        for(i=N-1;i>0;i--) {
            Swap(&A[0], &A[i]);
            PrecDown(A, 0, i);
            if(IsSameArr(A, B, N)) {
                printf("Heap Sort
    ");
                flag = 1;
                continue;
            }
            if(flag == 1) {
                flag = 0;
                PrintA(A, N);
            }
        }
    }
    
    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);
        if(!InsertionSort(Tmp, B, N)) {
            CopyArr(A, Tmp, N);
            Heap_Sort(Tmp, B, N);
        }
        return 0;
    }
     
    无欲速,无见小利。欲速,则不达;见小利,则大事不成。
  • 相关阅读:
    .net core之log4net和过滤器
    记录待掌握的.net组件或框架或技术
    Spring 注释 @Autowired 和@Resource
    Java——Cookie与Session
    Hadoop
    分布式系统!
    mybatis 学习!
    Java动态代理一Proxy
    JavaWeb学习--Servlet认识
    Angular.JS
  • 原文地址:https://www.cnblogs.com/ch122633/p/9024926.html
Copyright © 2020-2023  润新知