Insert or Merge
7-13 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<iostream>
using namespace std;
void Merge(int sequence1[],int l,int r,int rend,int temp[]){
int lend=r-;
int s=l;
while(l<=lend&&r<=rend){
if(sequence1[l]<sequence1[r]) temp[s++]=sequence1[l++];
else temp[s++]=sequence1[r++];
}
while(l<=lend) temp[s++]=sequence1[l++];
while(r<=rend) temp[s++]=sequence1[r++];
}
int main(){
int N;
int flag=;//Insertion_sort
cin>>N;
int sequence[N];
int sequence1[N];
for(int i=;i<N;i++)
cin>>sequence[i];
for(int i=;i<N;i++)
cin>>sequence1[i];
int i,j;
for(j=;j<N-;j++)
if(sequence1[j]>sequence1[j+]) break;
int sign=j;
for(j=j+;j<N;j++)
if(sequence[j]!=sequence1[j]) flag=;//Merge_sort
if(flag==){
cout<<"Insertion Sort"<<endl;
int temp=sequence1[sign+];
for(i=sign+;i>=;i--)
if(temp<sequence1[i-])
sequence1[i]=sequence1[i-];
else break;
sequence1[i]=temp;
int tag=;
for( i=;i<N;i++){
if(tag++==) cout<<sequence1[i];
else cout<<" "<<sequence1[i];
}
}
else{
int tag=;
cout<<"Merge Sort"<<endl;
int length;
for(length=;length<=N;length*=){
for(i=length-;i<N-;i+=*length)
if(sequence1[i]>sequence1[i+])
{tag=; break;}
if(tag==) break;
}
int temp[N];
for(i=;i<N-*length;i+=*length)
Merge(sequence1,i,i+length,i+*length-,temp);
if(i+length<N)
Merge(sequence1,i,i+length,N-,temp);
else
for(;i<N;i++) temp[i]=sequence[i];
int tick=;
for(int k=;k<N;k++)
if(tick++==) cout<<temp[k];
else cout<<" "<<temp[k];
cout<<endl;
}
return ;
}
Insert or Merge的更多相关文章
- PTA Insert or Merge
According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and gr ...
- 60. Insert Interval && Merge Intervals
Insert Interval Given a set of non-overlapping intervals, insert a new interval into the intervals ( ...
- 【题解】【区间】【二分查找】【Leetcode】Insert Interval & Merge Intervals
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...
- PAT甲级1089. Insert or Merge
PAT甲级1089. Insert or Merge 题意: 根据维基百科: 插入排序迭代,消耗一个输入元素每次重复,并增加排序的输出列表.每次迭代,插入排序从输入数据中删除一个元素,在排序列表中找到 ...
- PAT 1089 Insert or Merge[难]
1089 Insert or Merge (25 分) According to Wikipedia: Insertion sort iterates, consuming one input ele ...
- PAT1089. Insert or Merge
PAT1089. Insert or Merge 题目大意 给定一个初始序列src, 一个排序当中的序列tar, 问排序方式是 Insert Sort, 或者 Merge Sort. 并输出下一次迭代 ...
- pat1089. Insert or Merge (25)
1089. Insert or Merge (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Accor ...
- 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_A1089#Insert or Merge
Source: PAT A1089 Insert or Merge (25 分) Description: According to Wikipedia: Insertion sort iterate ...
随机推荐
- 51Nod 1179 最大的最大公约数(暴力大法好)
#include <iostream> #include <cstdio> #include <cstring> using namespace std; ; in ...
- Jquery | 基础 | 慕课网 | 元素选择器
getElementsByTagName方法得到页面所有的<div>元素 var divs = document.getElementsByTagName('div'); 与 同样的效果, ...
- win32 指令大全
指令类型 助记符 (带*为特权指令) 对标志寄存器的影响 备注 说明 举例 ZF CF PF SF OF AF DF IF TF 数据传送类 数据传送 MOV 不影响标志位 Move MOV r/m3 ...
- 跟我一起玩Win32开发(13):握手对话框
一提到对话框,相信对它熟悉的人不在少数,更不用说码农们了,你可能会问,对话框和窗口有什么区别吗?本质上是没有区别的,对话框也是一种窗口(前面也说过,控件也可视为子窗口). 最简单的对话框要数Messa ...
- Jmeter之一个请求获取上一个请求的参数
刚开始有这个需求,网上都是一些使用正则表达式的例子,苦于自己看不好正式的表达式,且响应结果稍微变一下,自己就不会写了,于是谷歌上各种搜,也阅读官网上文档,后来发现一个好的插件 Json path Ex ...
- 关于list,字符串的小记录
1.关于操作list的命令: a.append("hi") 这个可以在list的最后一项加上个这个字符串"hi",a是list的名字. del a[3] 删去l ...
- 491 Increasing Subsequences 递增子序列
给定一个整型数组, 你的任务是找到所有该数组的递增子序列,递增子序列的长度至少是2.示例:输入: [4, 6, 7, 7]输出: [[4, 6], [4, 7], [4, 6, 7], [4, 6, ...
- rhel6.5--http练习
包名 简介 httpd-2.2.15-29.el6_4.x86_64.rpm http服务的主程序包 httpd-devel-2.2.15-29.el6_4.x86_64.rpm ap ...
- 极其强大的运维工具——pscp、pssh、pslurp
1.pscp 用于将本地文件复制到远程主机 pscp -h xxx.host 本地文件 远程目录 //xxx.host是所有目的IP的文件,一个IP一行 2.pssh 在远程机器上执行命令 pssh ...
- IDEA下MyBatis错误总结
1. Pom.xml配置 语法顺序 <properties resource="config.properties"> </properties> < ...