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 ...
随机推荐
- 631D Messenger
题目大意 给你串s和t 但是每个串都被表示为多个二元组(x,y)表示字符x连续出现y次 问t在s中出现了多少次 分析 我们先将s和t每个串中二元组合并 即相邻两个二元组如果字符相等则将它们变为一个 特 ...
- ab工具进行压力测试
简介与安装 ab:Apache Benchmark,只要我们安装了Apache,就能够在Apache的安装目录中找到它. yum | apt 安装的Apache ab的目录一般为/usr/bin 也 ...
- Go-Mutex互斥量
先来看一段go1.12.5中Mutex的源码: // Copyright 2009 The Go Authors. All rights reserved. // Use of this source ...
- POJ 3281 网络流 拆点保证本身只匹配一对食物和饮料
如何建图? 最开始的问题就是,怎么表示一只牛有了食物和饮料呢? 后来发现可以先将食物与牛匹配,牛再去和饮料匹配,实际上这就构成了三个层次. 起点到食物层边的容量是1,食物层到奶牛层容量是1,奶牛层到饮 ...
- c# 调用 webService
开局几张 照着做就完事 说明下 这个wsdl 文件是根据别人提供的webService 接口 打开后改变后缀来的 这样就引用完成了 接下来就是重点了 怎么调用 localhost.WsSyncDu ...
- Linux中ssh及scp的连接
1. 当你想获取另外一台电脑上的数据时,可以使用这个命令 scp -P 10022 root@172.30.83.173:~/ubuntu1.tar ./ -r 代表传输文件夹,直接传文件可以不加 ...
- deque(双向队列)基本用法
deque(双向队列)基本用法 阅读体验:https://zybuluo.com/Junlier/note/1297030 简单介绍 就是可以两头插元素,两头删元素的数据结构 那么具体的STL操作(只 ...
- Yii中CreateUrl的使用总结
在Yii中经常要生成URL,不管是为了自动跳转还是仅仅是一个链接.下面对Yii中的URL生成做了一个总结.提示:以下controllerX代表控制器X,actionX代表方法X.在Controller ...
- Solution for NULL pointer dereference
•mmap_min_addr forbids users from mapping low addresses 1. First available in July 2007 2. Several c ...
- python学习第十七天字符串的创建和操作方法
字符串也是任何编程语言最常见的编程语言,字符串是有序的,可以通过下标来访问,可以切片,可以查找,可以替换,字符串可以和列表之间互相转换 join() split() 等函数 1,字符串的创建 单引号 ...