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

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 = ; 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 > && origin[i-] > temp; i-- )
origin[i] = origin[i-]; //依次与已排序序列中元素比较并右移
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 - ; /* 左边终点位置 */
int temp = L; /* 有序序列的起始位置 */
int NumElements = RightEnd - L + ; 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 = ; 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 = ; i <= N-*length; i += *length )
Merge( A, TmpA, i, i+length, i+*length- );
if ( i+length < N ) /* 归并最后2个子列*/
Merge( A, TmpA, i, i+length, N-);
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 = ; /* 初始化子序列长度*/
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 *= ;
Merge_pass( TmpA, A, N, length ); for(int i = ; i < N-; i++) //打印再一次排序后数组
printf("%d ",A[i]);
printf("%d\n",A[N-]);
return;
}
length *= ;
Merge_pass( TmpA, A, N, length );
if( Judge(TmpA,changed,N)) {
printf("Merge Sort\n");
length *= ;
Merge_pass( A, TmpA, N, length ); for(int i = ; i < N-; i++) //打印再一次排序后数组
printf("%d ",TmpA[i]);
printf("%d\n",TmpA[N-]);
return;
}
length *= ;
}
free( TmpA );
}
else printf( "空间不足\n" );
} int main()
{
int N;
int origin[],origin2[],changed[];
scanf("%d", &N);
for(int i = ; i < N; i++) { //origin origin1 初始序列
scanf("%d",&origin[i]);
origin2[i] = origin[i];
}
for(int i = ; i < N; i++) //changed 排序后序列
scanf("%d",&changed[i]); for(int i = ; i < N; i++) {
isInsertionSort(origin, N, i);
if( Judge(origin, changed,N) ) { //是插入排序
printf("Insertion Sort\n");
isInsertionSort(origin, N, i+);
for(int j = ; j < N-; j++)
printf("%d ",origin[j]);
printf("%d\n",origin[N-]);
return ;
}
} Merge_Sort( origin2, N , changed);
return ;
}

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

  1. PTA Insert or Merge

    According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and gr ...

  2. 60. Insert Interval && Merge Intervals

    Insert Interval Given a set of non-overlapping intervals, insert a new interval into the intervals ( ...

  3. 【题解】【区间】【二分查找】【Leetcode】Insert Interval & Merge Intervals

    Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...

  4. PAT甲级1089. Insert or Merge

    PAT甲级1089. Insert or Merge 题意: 根据维基百科: 插入排序迭代,消耗一个输入元素每次重复,并增加排序的输出列表.每次迭代,插入排序从输入数据中删除一个元素,在排序列表中找到 ...

  5. PAT1089. Insert or Merge

    PAT1089. Insert or Merge 题目大意 给定一个初始序列src, 一个排序当中的序列tar, 问排序方式是 Insert Sort, 或者 Merge Sort. 并输出下一次迭代 ...

  6. PAT甲级:1089 Insert or Merge (25分)

    PAT甲级:1089 Insert or Merge (25分) 题干 According to Wikipedia: Insertion sort iterates, consuming one i ...

  7. 排序合并连接(sort merge join)的原理

    排序合并连接(sort merge join)的原理 排序合并连接(sort merge join)的原理     排序合并连接(sort merge join)       访问次数:两张表都只会访 ...

  8. PAT 1089 Insert or Merge[难]

    1089 Insert or Merge (25 分) According to Wikipedia: Insertion sort iterates, consuming one input ele ...

  9. pat1089. Insert or Merge (25)

    1089. Insert or Merge (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Accor ...

  10. 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 ...

随机推荐

  1. php 通过PATH_SEPARATOR判断当前服务器系统类型

    PATH_SEPARATOR是php中的一个预定义常量,我们可以直接echo这个常量,在linux系统中,该常量输出":",在windows系统中,该常量输出";&quo ...

  2. Html类ImageGetter接口

    public class ImgLabelActivity extends Activity { private static final String TAG = "ImgLabelAct ...

  3. jmeter随笔(11)--上传文件接口出错

    点击标题下「飞测」可快速关注 坚持的是分享,搬运的是知识,图的是大家的进步,没有收费的培训,没有虚度的吹水,喜欢就关注.转发(免费帮助更多伙伴)等来交流,想了解的知识请留言,给你带来更多价值,是我们期 ...

  4. canvas-7globleCompositeOperation2.html

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. java.lang.NoSuchMethodException

    这个异常遇到过若干次,提示信息也比较清楚的指示出它的特点,当无法找到某一特定方法时,就会抛出该异常! 我所遇到的抛出此异常的情景主要有以下两种: 1:对应的JAVA类中没有对应的属性,也就是说在页面的 ...

  6. python --那些你应该知道的知识点

    1.python函数参数(含星号参数)http://blog.useasp.net/archive/2014/06/23/the-python-function-or-method-parameter ...

  7. GUI异步编程之BackgroundWorker类

    GUI编程中,经常需要另建一个线程,在后台运行以完成某项工作,并不时地与界面主线程进行通信,以改变界面显示.BackgroundWorker类为此而生. BackgroundWorker类的主要成员: ...

  8. PayPal 开发详解(五):创建第一个应用,获取clientId和clientSecret

    1.创建APP 2.填写应用名称->创建应用 3.获取 clientId 和 clientSecret

  9. javaSE第十一天

    第十一天    63 1:Eclipse的概述使用(掌握)    63 2:API的概述(了解)    63 3:Object类(掌握)    63       第十一天 1:Eclipse的概述使用 ...

  10. [Cocos2d-x for WP8学习笔记] HelloWorld结构分析

    先来看一下目录结构: Assets:游戏资源文件,图片音频等,Resource文件夹也有类似功能 include:用于放置游戏头文件 Shaders:渲染器着色器文件(大雾) cocos2dorig. ...