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=;i<N;i++) {
if(i != N-)
printf("%d ", A[i]);
else
printf("%d\n", A[i]);
}
} int IsSameArr(int A[], int C[], int N)
{
int i;
for(i=;i<N;i++)
if(A[i] != C[i]) return ;
return ;
} void CopyArr(int A[], int B[], int N)
{
int i;
for(i=;i<N;i++)
B[i] = A[i];
} void Read(int A[], int B[], int N)
{
int i;
for(i=;i<N;i++) {
if(i!=N-)
scanf("%d ", &A[i]);
else
scanf("%d\n", &A[i]);
} for(i=;i<N;i++) {
if(i!=N-)
scanf("%d ", &B[i]);
else
scanf("%d\n", &B[i]);
}
} void InsertionSort(int A[], int B[], int N)
{
int P, i;
int Tmp;
int flag = ; for(P=;P<N;P++) {
Tmp = A[P];
for(i=P;i> && A[i-]>Tmp;i--)
A[i] = A[i-];
A[i] = Tmp;
if(IsSameArr(A, B, N)) {
printf("Insertion Sort\n");
flag = ;
continue;
}
if(flag== ) {
flag = ;
PrintA(A, N);
}
}
} void Merge(int A[], int TmpA[], int L, int R, int RightEnd)
{
int LeftEnd = R - ;
int Tmp = L;
int NumElements = RightEnd - L + ;
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=;i<NumElements;i++, RightEnd--)
A[RightEnd] = TmpA[RightEnd];
} void Merge_Pass(int A[], int 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)
Merge(A, TmpA, i, i+length, N-);
else
for(j=i;j<N;j++) TmpA[j] = A[j]; } void Merge_Sort(int A[], int B[], int N)
{
int *TmpA;
int length = ;
int flag = ;
int flag2 = ;
TmpA = malloc(sizeof(int)*N);
if(TmpA != NULL) {
while(length < N) {
Merge_Pass(A, TmpA, N, length);
length *= ;
if(IsSameArr(A, B, N)) {
printf("Merge Sort\n");
flag = ;
}
if(flag2 == ) {
flag2 = ;
PrintA(A, N);
} Merge_Pass(TmpA, A, N, length);
length *= ;
if(IsSameArr(A, B, N)) {
printf("Merge Sort\n");
flag2 = ;
}
if(flag == ) {
flag = ;
PrintA(A, N);
}
}
free(TmpA);
} else
printf("Sapce not enough");
} int main()
{
int N;
int *A, *B, *Tmp;
scanf("%d\n", &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 ;
}
 

09-排序2 Insert or Merge(25 分)的更多相关文章

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

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

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

  3. A1089 Insert or Merge (25 分)

    一.技术总结 看到是一个two pointers问题,核心是要理解插入排序和归并排序的实现原理,然后判断最后实现 可以知道a数组和b数组怎么样判断是插入排序还是归并排序,因为插入排序是来一个排一个,所 ...

  4. 【PAT甲级】1089 Insert or Merge (25 分)(插入排序和归并排序)

    题意: 输入一个正整数N(<=100),接着输入两行N个整数,第一行表示初始序列,第二行表示经过一定程度的排序后的序列.输出这个序列是由插入排序或者归并排序得到的,并且下一行输出经过再一次排序操 ...

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

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

  6. 1089 Insert or Merge (25 分)

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

  7. 1089 Insert or Merge (25分)

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

  8. 7-19(排序) 寻找大富翁 (25 分)(归并排序)(C语言实现)

    7-19(排序) 寻找大富翁 (25 分) 胡润研究院的调查显示,截至2017年底,中国个人资产超过1亿元的高净值人群达15万人.假设给出N个人的个人资产值,请快速找出资产排前M位的大富翁. 输入格式 ...

  9. pat1089. Insert or Merge (25)

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

  10. PAT 1089. Insert or Merge (25)

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

随机推荐

  1. 535D Tavas and Malekas

    题目大意 给你一个串和m个下标 问你一个长度为n的串每一个下标开始的后缀的前缀都包含给定的串的方案数 分析 对于给定的串求出z数组 对于两个串不重叠的情况就是中间都不包含的数随便填即可 对于重叠的情况 ...

  2. DJango安装-windows

    1.进入虚拟环境后启动 activate 2.查看当前虚拟环境是否存在Django环境 pip list 3.不存在则 安装Django环境 pip install django 4.查看Django ...

  3. Spark SQL实战

    一.程序 package sparklearning import org.apache.log4j.Logger import org.apache.spark.SparkConf import o ...

  4. python web自动化测试框架搭建(功能&接口)——接口公共方法

    接口公共方法有:数据引擎.http引擎.Excel引擎 1.数据引擎:获取用例.结果检查.结果统计 # -*- coding:utf-8 -*- from XlsEngine import XlsEn ...

  5. python中进程池和回调函数

    一.数据共享 1.进程间的通信应该尽量避免共享数据的方式 2.进程间的数据是独立的,可以借助队列或管道实现通信,二者都是基于消息传递的. 虽然进程间数据独立,但可以用过Manager实现数据共享,事实 ...

  6. python常用模块----re模块

    正则表达式就是匹配字符串内容的一种规则. 字符组: [0123456789] 表示0-9这个范围内的任意一个数字都可以与之匹配,简写为[0-9] [a-z] 表示匹配所有的小写字母 [A-Z] 表示匹 ...

  7. opencv2——图像上的算术运算4

    1.图像算术运算 参数含义: src1:第一张图像 src2:第二张图像 dst:destination,目标图像,需要提前分配空间,可省略 mask:掩膜 scale:缩放比,常量 dtype:数据 ...

  8. 56-python基础-python3-集合-新建集合

    集合对象是一组无序排列的可哈希的值,集合成员可以做字典中的键. 集合支持用in和not in操作符检查成员. 由len()内建函数得到集合的基数(大小). 用 for 循环迭代集合的成员. 但是因为集 ...

  9. LeetCode 852. Peak Index in a Mountain Array(C++)

    Let's call an array A a mountain if the following properties hold: A.length >= 3 There exists som ...

  10. ELK + Filebeat日志分析系统安装

    之前搭建过elk,用于分析日志,无奈服务器资源不足,开了多个Logstash之后发现占用内存过高,于是现在改为Filebeat做日志收集,记录一下搭建过程和遇到问题的解决方案. 第一步 , 安装jdk ...