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 ...
随机推荐
- python 数字系列-复数的数学运算
复数的数学运算 问题 你写的最新的网络认证方案代码遇到了一个难题,并且你唯一的解决办法就是使用复数空间. 再或者是你仅仅需要使用复数来执行一些计算操作. 解决方案 复数可以用使用函数 complex( ...
- selenuim&PhantomJS&Beautifulsoup练习经典实例
# coding = utf-8__autor__ = 'litao' from selenium import webdriverfrom selenium.webdriver.common.by ...
- 牛客提高D6t1 积木大赛
分析 每次修改用二位差分记录一下 之后对于三维分别统计即可 代码 #include<iostream> #include<cstdio> #include<cstring ...
- MAC 安装jenkins
下载地址 : https://jenkins.io/zh/download/ 由于用dmg安装包去安装jenkins,Jenkins不会用本地的用户去构建,任何创建的文件都是“jenkins”用户所有 ...
- day05—JavaScript之函数调用
转行学开发,代码100天——2018-03-21 JavaScript中的函数调用有4种方式: 方式一:直接通过函数名调用 在 HTML 中默认的全局对象是 HTML 页面本身,所以函数是属于 HTM ...
- SqlSession 内部运行
<深入浅出MyBatis技术原理与实战>p150页 SqlSession内部运行图 四大对象在流程中的操作. 1.准备sql.StatementHandler 的prepare方法进行sq ...
- 神经网络 fann 教程 英文 以及 翻译 参考
http://fann.sourceforge.net/fann_en.pdf http://blog.csdn.net/fengshuiyue/article/details/41446257
- Jmeter 循环控制器 遍历结果
1.测试计划,添加Mysql jar包 2.线程组 3.JDBC Connection Configuration,配置Mysql 4.添加JDBC Request,将查询出的数据对应的存入usern ...
- Bootstrap 学习笔记9 标签页和工具提示插件
<ul class="nav nav-tabs"> <li class="active"><a href="#html5 ...
- [LeetCode] 421. Maximum XOR of Two Numbers in an Array(位操作)
传送门 Description Given a non-empty array of numbers, a0, a1, a2, … , an-1, where 0 ≤ ai < 231. Fin ...