09-排序2 Insert or Merge

要点就是把排序每一步,判断一下是否和第二组数据相同,若相同则输出排序方法和下一次序列。

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

中文题目:

根据维基百科的定义:

插入排序是迭代算法,逐一获得输入数据,逐步产生有序的输出序列。每步迭代中,算法从输入序列中取出一元素,将之插入有序序列中正确的位置。如此迭代直到全部元素有序。

归并排序进行如下迭代操作:首先将原始序列看成N个只包含1个元素的有序子序列,然后每次迭代归并两个相邻的有序子序列,直到最后只剩下1个有序的序列。

现给定原始序列和由某排序算法产生的中间序列,请你判断该算法究竟是哪种排序算法?

输入格式:

输入在第一行给出正整数N (<=100);随后一行给出原始序列的N个整数;最后一行给出由某排序算法产生的中间序列。这里假设排序的目标序列是升序。数字间以空格分隔。

输出格式:

首先在第1行中输出“Insertion Sort”表示插入排序、或“Merge Sort”表示归并排序;然后在第2行中输出用该排序算法再迭代一轮的结果序列。题目保证每组测试的结果是唯一的。数字间以空格分隔,且行末不得有多余空格。
#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;

typedef int ElementType;
bool Judge(int origin[], int changed[], int len)
{
    for(int i = 0; i < len; i++)  {
        if(origin[i] != changed[i])
            return false;
    }
    return true;
} 

//插入排序 (需排序数组,数组长度,排序次数)
void isInsertionSort(ElementType origin[], int N, int times)
{
    int i;
    ElementType temp = origin[times];     //取出未排序序列中的第一个元素
    for (i = times; i > 0 && origin[i-1] > temp; i-- )
        origin[i] = origin[i-1];             //依次与已排序序列中元素比较并右移
    origin[i] = temp;
}

/* L = 左边起始位置, R = 右边起始位置, RightEnd = 右边终点位置*/
void Merge( ElementType A[], ElementType TmpA[], int L, int R, int RightEnd )
{ /* 将有序的A[L]~A[R-1]和A[R]~A[RightEnd]归并成一个有序序列 */

     int LeftEnd = R - 1; /* 左边终点位置 */
     int temp = L;         /* 有序序列的起始位置 */
     int NumElements = RightEnd - L + 1;

     while( L <= LeftEnd && R <= RightEnd ) {
         if ( A[L] <= A[R] )
             TmpA[temp++] = A[L++]; /* 将左边元素复制到TmpA */
         else
             TmpA[temp++] = A[R++]; /* 将右边元素复制到TmpA */
     }

     while( L <= LeftEnd )
         TmpA[temp++] = A[L++]; /* 直接复制左边剩下的 */
     while( R <= RightEnd )
         TmpA[temp++] = A[R++]; /* 直接复制右边剩下的 */

     for(int i = 0; i < NumElements; i++, RightEnd -- )
         A[RightEnd] = TmpA[RightEnd]; /* 将有序的TmpA[]复制回A[] */
}

/* length = 当前有序子列的长度*/
/* 两两归并相邻有序子列 */
void Merge_pass( ElementType A[], ElementType 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 ) /* 归并最后2个子列*/
         Merge( A, TmpA, i, i+length, N-1);
     else /* 最后只剩1个子列*/
         for ( j = i; j < N; j++ ) TmpA[j] = A[j];
}

void Merge_Sort( ElementType A[], int N ,int changed[])
{
     int length;
     ElementType *TmpA;

     length = 1; /* 初始化子序列长度*/
     TmpA = (ElementType *)malloc( N * sizeof( ElementType ) );
     if ( TmpA != NULL ) {
          while( length < N ) {
              Merge_pass( A, TmpA, N, length );
              if( Judge(TmpA,changed,N)) {        //是归并排序
                  printf("Merge Sort\n");
                  length *= 2;
                  Merge_pass( TmpA, A, N, length );

                  for(int i = 0; i < N-1; i++)     //打印再一次排序后数组
                      printf("%d ",A[i]);
                  printf("%d\n",A[N-1]);
                  return;
              }
              length *= 2;
              Merge_pass( TmpA, A, N, length );
              if( Judge(TmpA,changed,N)) {
                  printf("Merge Sort\n");
                  length *= 2;
                  Merge_pass( A, TmpA, N, length );

                  for(int i = 0; i < N-1; i++)     //打印再一次排序后数组
                      printf("%d ",TmpA[i]);
                  printf("%d\n",TmpA[N-1]);
                  return;
              }
              length *= 2;
          }
          free( TmpA );
     }
     else printf( "空间不足\n" );
}

int main()
{
    int N;
    int origin[105],origin2[105],changed[105];
    scanf("%d", &N);
    for(int i = 0; i < N; i++) {    //origin origin1 初始序列
        scanf("%d",&origin[i]);
        origin2[i] = origin[i];
    }
    for(int i = 0; i < N; i++)         //changed 排序后序列
        scanf("%d",&changed[i]);

    for(int i = 1; i < N; i++) {
        isInsertionSort(origin, N, i);
        if( Judge(origin, changed,N) ) {    //是插入排序
            printf("Insertion Sort\n");
            isInsertionSort(origin, N, i+1);
            for(int j = 0; j < N-1; j++)
                printf("%d ",origin[j]);
            printf("%d\n",origin[N-1]);
            return 0;
        }
    } 

    Merge_Sort( origin2, N , changed);
    return 0;
}
时间: 2024-10-19 07:39:01

09-排序2 Insert or Merge的相关文章

1089. Insert or Merge (25)【排序】——PAT (Advanced Level) Practise

题目信息 1089. Insert or Merge (25) 时间限制200 ms 内存限制65536 kB 代码长度限制16000 B 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, find

60. Insert Interval &amp;&amp; Merge Intervals

Insert Interval Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary). You may assume that the intervals were initially sorted according to their start times. Example 1: Given intervals [1,3],[6,9], i

07-2. Insert or Merge (25)

07-2. Insert or Merge (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and growing a sorted output list. Each iteration, insertion so

pat1089. Insert or Merge (25)

1089. Insert or Merge (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and growing a sorted output list. Each iteration, insertion s

Insert or Merge

h2.western { font-family: "Liberation Sans", sans-serif; font-size: 16pt } h2.cjk { font-size: 16pt } h2.ctl { font-size: 16pt } h1 { margin-bottom: 0.21cm } h1.western { font-family: "Liberation Sans", sans-serif; font-size: 18pt } h1

PTA 09-排序2 Insert or Merge (25分)

题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/675 5-13 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 rem

1089. Insert or Merge (25)

1089. Insert or Merge (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and growing a sorted output list. Each iteration, insertion s

PAT_A1089#Insert or Merge

Source: PAT A1089 Insert or Merge (25 分) Description: 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 da

PAT 1088 1089. Insert or Merge (25)(排序啊)

题目链接:http://www.patest.cn/contests/pat-a-practise/1089 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 d

排序算法--归并排序(merge)

归并排序是利用递归和分而治之的技术将数据序列划分成为越来越小的半子表,再对半子表排序,最后再用递归步骤将排好序的半子表合并成为越来越大的有序序列,归并排序包括两个步骤,分别为: 1)划分子表  2)合并半子表 时间复杂度是Θ(nlgn),优于插入排序算法. 算法描述    1) 申请空间,使其大小为两个已经排序序列之和,该空间用来存放合并后的序列    2) 设定两个指针,最初位置分别为两个已经排序序列的起始位置    3) 比较两个指针所指向的元素,选择相对小的元素放入到合并空间,并移动指针到