1089 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 (≤). 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
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
int A[];
int B[];
int L;
void Print(int N)
{
int i;
for (i = ; i < N - ; i++)
printf("%d ", B[i]);
printf("%d", B[i]);
}
int IsOrder(int B[],int lo, int hi)
{
for (int i = lo; i < hi - ; i++)
if (B[i] > B[i + ])
return ;
return ;
}
int ChargeLength(int B[], int N, int Length) //检查以Length为长度的序列
{
int i;
for (i = ; i < N -Length; i +=Length)
if (!IsOrder(B, i, i+Length))
return ;
if (i< N)
if (!IsOrder(B, i, N))
return ;
L = Length;
return ;
}
int Charge(int B[], int N)
{
int Length = ;
while(Length <=N)
{
if (ChargeLength(B, N, Length))
Length *= ;
else
if (Length > )
return ;
else
return ;
}
} void Insert_Once(int B[],int i,int N)
{
int j;
int Temp = B[i];
for (j = i; j > && B[j - ] >Temp; j--)
B[j] = B[j - ];
B[j] = Temp;
} void Merge(int lo, int mi, int hi)
{
int* B1 = (int*)malloc(sizeof(int) * (mi - lo));
for (int i = ; i < mi - lo; i++)B1[i] = B[lo + i];
int i, j, k;
i = lo;
j = ;
k = mi;
while (j < mi - lo)
{
if (k >= hi || B1[j] <= B[k])B[i++] = B1[j++];
if (k<hi && B1[j]>B[k])B[i++] = B[k++];
}
free(B1);
}
void Merge_Once(int B[], int Length, int N)
{
int i;
for (i = ; i < N - * Length; i += * Length)
Merge(i, i + Length, i + * Length);
if (i + Length < N)
Merge(i, i + Length, N);
} int main()
{
int N;
int Flag = ;
int OrderPosition = ;
scanf("%d", &N);
for (int i = ; i < N; i++)
scanf("%d", &A[i]);
for (int i = ; i < N; i++)
scanf("%d", &B[i]);
if (Flag=Charge(B, N))
printf("Merge Sort\n");
else
printf("Insertion Sort\n");
for (int i = ; i < N - ; i++)
if (B[i] <=B[i + ]) //这里必须是大于等于
OrderPosition = i + ;
else
break;
if (Flag)
Merge_Once(B, L, N);
else
Insert_Once(B, OrderPosition+, N);
Print(N);
return ;
}
1089 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 ...
- 1089 Insert or Merge (25 分)
According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and gr ...
- 【PAT甲级】1089 Insert or Merge (25 分)(插入排序和归并排序)
题意: 输入一个正整数N(<=100),接着输入两行N个整数,第一行表示初始序列,第二行表示经过一定程度的排序后的序列.输出这个序列是由插入排序或者归并排序得到的,并且下一行输出经过再一次排序操 ...
- 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 ...
- PAT 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, ...
- 1089. Insert or Merge (25)-判断插入排序还是归并排序
判断插入排序很好判断,不是的话那就是归并排序了. 由于归并排序区间是2.4.8开始递增的,所以要判断给出的归并排序执行到哪一步,就要k从2开始枚举. 然后再对每个子区间进行一下sort即可. #inc ...
- PAT (Advanced Level) 1089. Insert or Merge (25)
简单题.模拟一下即可. #include<cstdio> #include<cstring> #include<cmath> #include<vector& ...
- A1089 Insert or Merge (25 分)
一.技术总结 看到是一个two pointers问题,核心是要理解插入排序和归并排序的实现原理,然后判断最后实现 可以知道a数组和b数组怎么样判断是插入排序还是归并排序,因为插入排序是来一个排一个,所 ...
随机推荐
- PHP变量存储结构
首先声明,我并没有去读PHP的源码,只是对于PHP的有时候诡异的表现感兴趣,找了一下开发人员laruence的博客结合PHP提供的函数debug_zval_dump刺探得到了本博客所阐述的工作机理.如 ...
- CentOS RPM 安装 MySQL5.7
环境 CentOS 7 64位 MySQL 5.7 64位 1.卸载系统自带的 mariadb [root@localhost /]# rpm -qa|grep mariadb mariadb-lib ...
- 解决mongo单文档超过16M
mongodb导入大文件的数据时,导入一小部分后,提示lost connect,失去连接.mongo文件有6.3G,网上查了一下,原来Mongo对单次处理好像有大小限制(16m),所以大文件会出问题, ...
- jQuery实现鼠标移入切换图片
初始效果: 鼠标移入效果: 首先添加jQuery库,我这边直接引用百度CDN地址 <script src="https://apps.bdimg.com/libs/jquery/2.1 ...
- 解决unrecognized import path "xxx"
$ export GOPROXY=https://goproxy.io 环境变量配置上面这句即可 https://goproxy.io 是一个goproxy.io这个开源项目提供的公开代理服务. 使用 ...
- 我的Keras使用总结(1)——Keras概述与常见问题整理
今天整理了自己所写的关于Keras的博客,有没发布的,有发布的,但是整体来说是有点乱的.上周有空,认真看了一周Keras的中文文档,稍有心得,整理于此.这里附上Keras官网地址: Keras英文文档 ...
- .Net Framework 工具Mpgo.exe与Ngen.exe
首先放出官方MSDN的文档地址 Mpgo.exe 主要用于分析程序集启动时需要哪些东西,然后将信息反馈给NGen.exe 来更好的优化本机映像,使得应用程序启动更快,工作集缩小.准备发布时,用MPGO ...
- 记一次:Windows的Socket编程学习和分析过程
Socket编程依赖于:WS2_32.dll --- 服务端 --- .导入我们需要的函数 #incldue <windows.h> //#include<WinSock2.h> ...
- 欲善事先利器-IEAD插件篇
工欲善其事,必先利其器,好鞋踢好球是非常合乎逻辑的事情. --<长江七号> 同样的开场白,不一样的酒,不一样的故事. 上篇<欲善事先利器--系统篇>已经推荐了一些个人常用的效率 ...
- [组件封装]微信小程序-图片批量上传照片墙
描述 批量上传图片, 可设置最大上传个数, 可删除, 可设置默认值. 效果 源码 pictures-wall.wxml <view class="picturesWall"& ...