zoukankan      html  css  js  c++  java
  • Insert or Merge && Insertion or Heap Sort

    原题连接:https://pta.patest.cn/pta/test/1342/exam/4/question/27102

    题目如下:

    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 NNN (≤100le 100100). Then in the next line, NNN integers are given as the initial sequence. The last line contains the partially sorted sequence of the NNN 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
    ______________________________________________________________________________________________________________________________________________________________________________
      感觉这道题目的题意比较理解,两种排序方法,但给出的样例是排序到其中某一排序的中间一步,至于是哪一步,还得自己写程序判断。以下是代码,其中某些地方也借鉴了其他人的写法,但记不清是哪位同学的了……
     1 #include<stdio.h>
     2 #define Max 105
     3 int IsSort(int *a,int *b,int N)
     4 {
     5     int i;
     6     for (i=0;i<N;i++)
     7     {
     8         if (a[i]!=b[i])return 0;
     9     }
    10     return 1;
    11 }
    12 
    13 void InsertSort(int A[],int p)
    14 {
    15     int tmp=A[p];
    16     int i;
    17     for (i=p;i>0 && A[i-1]>tmp;i--)A[i]=A[i-1];
    18     A[i]=tmp;
    19 }
    20 
    21 void Merge(int A[],int tmpA[],int L,int R,int RightEnd)
    22 {
    23     int LeftEnd,Number,tmp,i;
    24     LeftEnd=R-1;
    25     Number=RightEnd-L+1;
    26     tmp=L;
    27     while (L<=LeftEnd && R<=RightEnd)
    28     {
    29         if (A[L]<=A[R])tmpA[tmp++]=A[L++];
    30         else tmpA[tmp++]=A[R++];
    31     }
    32     while (L<=LeftEnd)tmpA[tmp++]=A[L++];
    33     while (R<=RightEnd)tmpA[tmp++]=A[R++];
    34     for (i=0;i<Number;i++,RightEnd--)
    35     {
    36         A[RightEnd]=tmpA[RightEnd];
    37     }
    38 
    39 }
    40 
    41 void MSort(int A[],int tmpA[],int length,int N)
    42 {
    43     int i,j;
    44     for (i=0;i<=N-2*length;i+=2*length)
    45     {
    46         Merge(A,tmpA,i,i+length,i+2*length-1);
    47     }
    48     if (i+length<N)Merge(A,tmpA,i,i+length,N-1);
    49 
    50 }
    51 
    52 void pri(int *a,int N)
    53 {
    54     int i;
    55     for (i=0;i<N;i++)
    56     {
    57         if (i==0)printf("%d",a[i]);
    58         else printf(" %d",a[i]);
    59     }
    60 
    61 }
    62 int main()
    63 {
    64     int N,a1[Max],a2[Max],b[Max],tmpA[Max],i,v;
    65     scanf("%d",&N);
    66     for (i=0;i<N;i++)
    67     {
    68         scanf("%d",&v);
    69         a1[i]=a2[i]=v;
    70     }
    71     for (i=0;i<N;i++)scanf("%d",&b[i]);
    72 
    73     for (i=1;i<N;i++)
    74     {
    75         InsertSort(a1,i);
    76         if (IsSort(a1,b,N))
    77         {
    78             printf("Insertion Sort
    ");
    79             InsertSort(a1,i+1);
    80             pri(a1,N);
    81             return 0;
    82         }
    83     }
    84 
    85     int length=1;
    86     while (length<N){
    87         MSort(a2,tmpA,length,N);
    88         length*=2;
    89         if (IsSort(a2,b,N))
    90         {
    91             printf("Merge Sort
    ");
    92             MSort(a2,tmpA,length,N);
    93             pri(a2,N);
    94             return 0;
    95         }
    96     }
    97 
    98 }

     ________________________________________________________________________________________________________________________

    接下来还有一道题和该题类似,只不过把归并排序换成了堆排序

      1 #include<stdio.h>
      2 #define Max 100
      3 int IsSort(int *a,int *b,int N)
      4 {
      5     int i;
      6     for (i=0;i<N;i++)
      7     {
      8         if (a[i]!=b[i])return 0;
      9     }
     10     return 1;
     11 }
     12 
     13 int InsertionSort(int *a,int p)
     14 {
     15     int i,tmp;
     16     tmp=a[p];
     17     for (i=p;i>0 && a[i-1]>tmp ;i--)
     18     {
     19         a[i]=a[i-1];
     20     }
     21     a[i]=tmp;
     22 }
     23 
     24 void Swap(int *a,int *b)
     25 {
     26     int tmp;
     27     tmp=*a;*a=*b;*b=tmp;
     28 }
     29 
     30 void PercDown(int *a,int p,int N)
     31 {
     32     int tmp,Child,Parent;
     33     tmp=a[p];
     34     for (Parent=p;Parent*2+1<N;Parent=Child)
     35     {
     36         Child=Parent*2+1;
     37         if ((Child!=N-1) && (a[Child+1]>a[Child]))Child++;
     38         if (tmp>=a[Child])break;
     39         else a[Parent]=a[Child];
     40     }
     41     a[Parent]=tmp;
     42 }
     43 
     44 /* void HeapSort(int *a,int N)
     45 {
     46     int i;
     47     for (i=N/2-1;i>=0;i--)PercDown(a,i,N);
     48     for (i=N-1;i>0;i--)
     49     {Swap(&a[0],&a[i]);
     50     PercDown(a,0,i);}
     51 } */
     52 
     53 void Pri(int *a,int N)
     54 {
     55     int i;
     56     for (i=0;i<N;i++)
     57     {
     58         if (i==0)printf("%d",a[i]);
     59         else printf(" %d",a[i]);
     60     }
     61 }
     62 
     63 int main()
     64 {
     65     int i,N,a1[Max],a2[Max],b[Max],v;
     66     scanf("%d",&N);
     67     for (i=0;i<N;i++)
     68     {
     69         scanf("%d",&v);
     70         a1[i]=a2[i]=v;
     71     }
     72     for (i=0;i<N;i++)scanf("%d",&b[i]);
     73 
     74     for (i=0;i<N;i++)
     75     {
     76         InsertionSort(a1,i);
     77         if (IsSort(a1,b,N))
     78         {
     79             printf("Insertion Sort
    ");
     80             InsertionSort(a1,++i);
     81             Pri(a1,N);
     82             return 0;
     83         }
     84     }
     85 
     86     for (i=N/2-1;i>=0;i--)PercDown(a2,i,N); //Build the Heap;
     87 
     88     for (i=N-1;i>0;i--)
     89     {
     90         Swap(&a2[0],&a2[i]);
     91         PercDown(a2,0,i);
     92         if (IsSort(a2,b,N))
     93         {
     94         printf("Heap Sort
    ");
     95         Swap(&a2[0],&a2[--i]);
     96         PercDown(a2,0,i);
     97         Pri(a2,N);
     98             return 0;
     99         }
    100         else printf("df");
    101     }
    102 }
  • 相关阅读:
    XAML实例教程系列
    XAML实例教程系列
    XAML实例教程系列
    正则表达式 修改流程 过程是崎岖的
    Codeforces Round #379 (Div. 2) 解题报告
    (DFS)codevs1004-四子连棋
    (BFS)poj2935-Basic Wall Maze
    (BFS)poj1465-Multiple
    (BFS)uva2554-Snakes & Ladders
    (BFS)hdoj2377-Bus Pass
  • 原文地址:https://www.cnblogs.com/wuxiaotianC/p/6136364.html
Copyright © 2011-2022 走看看