09-排序2 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 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 分)的更多相关文章
- PAT甲级:1089 Insert or Merge (25分)
PAT甲级:1089 Insert or Merge (25分) 题干 According to Wikipedia: Insertion sort iterates, consuming one i ...
- 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 ...
- A1089 Insert or Merge (25 分)
一.技术总结 看到是一个two pointers问题,核心是要理解插入排序和归并排序的实现原理,然后判断最后实现 可以知道a数组和b数组怎么样判断是插入排序还是归并排序,因为插入排序是来一个排一个,所 ...
- 【PAT甲级】1089 Insert or Merge (25 分)(插入排序和归并排序)
题意: 输入一个正整数N(<=100),接着输入两行N个整数,第一行表示初始序列,第二行表示经过一定程度的排序后的序列.输出这个序列是由插入排序或者归并排序得到的,并且下一行输出经过再一次排序操 ...
- 09-排序2 Insert or Merge (25 分)
According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and gr ...
- 1089 Insert or Merge (25 分)
According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and gr ...
- 1089 Insert or Merge (25分)
According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and gr ...
- 7-19(排序) 寻找大富翁 (25 分)(归并排序)(C语言实现)
7-19(排序) 寻找大富翁 (25 分) 胡润研究院的调查显示,截至2017年底,中国个人资产超过1亿元的高净值人群达15万人.假设给出N个人的个人资产值,请快速找出资产排前M位的大富翁. 输入格式 ...
- pat1089. Insert or Merge (25)
1089. Insert or Merge (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Accor ...
- PAT 1089. Insert or Merge (25)
According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and gr ...
随机推荐
- jmeter之登录接口的一次简单压测与分析
前言:登录接口的一次简单压测与分析 1.接口文档 2.配置元件 3.结果分析 1.接口文档 a.拿到接口文档 接口地址:http://localhost:8080/jpress/admin/login ...
- mariadb(二)增删改
一.表的结构的增删改 添加数据类型 alter table 表名 add 字段 数据类型: #如果字段存在则会报错 Duplicate column name '字段' #修改数据类型 alter t ...
- mysql 5.1.34
在make之前,将MAKEFILE中的do abi check注释,不要注释名字... mysql 5.1 编译安装 分类: mysql2012-04-06 13:01 17175人阅读 评论(0) ...
- mooc-IDEA 应用快捷键自动创建测试类--010
十六.IntelliJ IDEA -应用快捷键自动创建测试类 Step1:在类或接口上,按ctrl+shift+t 选择Create New Test... 则在相应测试包下.创建该测试类. 测试类:
- python while 循环打印九九乘法表
方向一 i = 1 while i <= 9: j = 1 while j <= i print('%d*%d = %2d'%( j,i ,i*j),end='') j += 1 prin ...
- oracle函数与存储方法
oracle中的函数, 可以理解为java中的方法 有参数, 或者没有参数 通过return返回一个值 oracle存储过程跟函数唯一的区别, 存储过程不能通过return返回一个值 参数的类型, i ...
- zabbix 微信告警配置
作者信息 邮箱:sijiayong000@163.com Q Q:601566386 Zabbix 微信告警 摘要:Zabbix可以通过多种方式把告警信息发送到指定人,常用的有邮件,短信报警方式,但是 ...
- MySQL-第八篇MySQL内置函数
1.根据函数对多行数据的处理方式,可以分为: 1>单行函数:对每行输入值进行单独计算,每行得到一个计算结果返回给用户. 2>多行函数:聚集函数.分组函数,主要用于完成一些统计功能.对多行 ...
- [Codeforces 280D]k-Maximum Subsequence Sum(线段树)
[Codeforces 280D]k-Maximum Subsequence Sum(线段树) 题面 给出一个序列,序列里面的数有正有负,有两种操作 1.单点修改 2.区间查询,在区间中选出至多k个不 ...
- python eval( ) 使用详解
1.解析表达式 (表达式是str类型)----最常用 a = 12 b = "联播" result1 = eval(a+3) # resu ...