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数组怎么样判断是插入排序还是归并排序,因为插入排序是来一个排一个,所 ...
随机推荐
- java算法--稀疏数组
数据结构必要知识 线性结构 线性结构是最常用的数据结构,数据元素之间存在一对一的线性关系. 线性结构有两种不同的存储结构,即顺序存储结构和链式存储结构.顺序存储的线性表称为顺序表,顺序表中的存储元素是 ...
- Mysql(Mariadb)慢查询日志中long_query_time 与log_queries_not_using_indexes与min_examined_row_limit 关系分析
慢查询日志中long_query_time 与log_queries_not_using_indexes与min_examined_row_limit 关系分析 参数介绍: long_query_ ...
- 【5min+】 一个令牌走天下!.Net Core中的ChangeToken
系列介绍 [五分钟的dotnet]是一个利用您的碎片化时间来学习和丰富.net知识的博文系列.它所包含了.net体系中可能会涉及到的方方面面,比如C#的小细节,AspnetCore,微服务中的.net ...
- AspNetCore3.1_Secutiry源码解析_2_Authentication_核心对象
系列文章目录 AspNetCore3.1_Secutiry源码解析_1_目录 AspNetCore3.1_Secutiry源码解析_2_Authentication_核心项目 AspNetCore3. ...
- Mol Cell Proteomics. | Identification of salivary biomarkers for oral cancer detection with untargeted and targeted quantitative proteomics approaches (解读人:卜繁宇)
文献名:Identification of salivary biomarkers for oral cancer detection with untargeted and targeted qua ...
- Java并发编程之CAS第三篇-CAS的缺点及解决办法
Java并发编程之CAS第三篇-CAS的缺点 通过前两篇的文章介绍,我们知道了CAS是什么以及查看源码了解CAS原理.那么在多线程并发环境中,的缺点是什么呢?这篇文章我们就来讨论讨论 本篇是<凯 ...
- hdu1226超级密码 bfs
题目链接:http://icpc.njust.edu.cn/Problem/Hdu/1226/ 题目大意是:寻找一个五百位之内的C进制密码,该密码是N的正整数倍,而且只能用给定的数构成密码,求这样的密 ...
- Javascript函数之深入浅出递归思想
一.递归函数的理解 1.生活中的递归 "递归"在生活中的一个典例就是"问路".如图小哥哥进入电影院后找不到自己的座位,问身边的小姐姐"这是第几排&qu ...
- Spring Cloud 系列之 Netflix Zuul 服务网关
什么是 Zuul Zuul 是从设备和网站到应用程序后端的所有请求的前门.作为边缘服务应用程序,Zuul 旨在实现动态路由,监视,弹性和安全性.Zuul 包含了对请求的路由和过滤两个最主要的功能. Z ...
- Xamarin.Forms读取并展示Android和iOS通讯录 - TerminalMACS客户端
Xamarin.Forms读取并展示Android和iOS通讯录 - TerminalMACS客户端 本文同步更新地址: https://dotnet9.com/11520.html https:// ...