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的更多相关文章

  1. PTA Insert or Merge

    According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and gr ...

  2. 60. Insert Interval && Merge Intervals

    Insert Interval Given a set of non-overlapping intervals, insert a new interval into the intervals ( ...

  3. 【题解】【区间】【二分查找】【Leetcode】Insert Interval & Merge Intervals

    Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...

  4. PAT甲级1089. Insert or Merge

    PAT甲级1089. Insert or Merge 题意: 根据维基百科: 插入排序迭代,消耗一个输入元素每次重复,并增加排序的输出列表.每次迭代,插入排序从输入数据中删除一个元素,在排序列表中找到 ...

  5. PAT 1089 Insert or Merge[难]

    1089 Insert or Merge (25 分) According to Wikipedia: Insertion sort iterates, consuming one input ele ...

  6. PAT1089. Insert or Merge

    PAT1089. Insert or Merge 题目大意 给定一个初始序列src, 一个排序当中的序列tar, 问排序方式是 Insert Sort, 或者 Merge Sort. 并输出下一次迭代 ...

  7. pat1089. Insert or Merge (25)

    1089. Insert or Merge (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Accor ...

  8. 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 ...

  9. PAT_A1089#Insert or Merge

    Source: PAT A1089 Insert or Merge (25 分) Description: According to Wikipedia: Insertion sort iterate ...

随机推荐

  1. popoverController使用注意--转

    一.设置尺寸 提示:不建议,像下面这样吧popover的宽度和高度写死. 1 //1.新建一个内容控制器 2 YYMenuViewController *menuVc=[[YYMenuViewCont ...

  2. java中的compareto方法以及LIst列表排序的详细介绍【转】

    java中的compareto方法的详细介绍 javacompareTo  java中的compareto方法,返回参与比较的前后两个字符串的asc码的差值,看下面一组代码 String a=&quo ...

  3. java之idea打jar包

    1.选中项目右键,选择Open module settings,如图: 2.点击Artifacts,添加jar,如图: 3.create jar,必须选择项目的更目录 4.build artfacts ...

  4. QQ面板拖拽(慕课网DOM事件探秘)(上)

    QQ面板拖拽,效果如图 JavaScript代码如下: function getByClass(clsName, parent) { var oParent = parent ? document.g ...

  5. IOS状态栏

    IOS状态栏是什么地方? 它是IOS设备屏幕顶部显示信号以及电池的区域.状态栏默认的高度是20像素,状态栏在软件开发中有何作用?联网应用中可在自动帮用户下载数据时使用,推荐在状态栏中予以显示.状态栏可 ...

  6. 飞秋软件的OA消息接口服务器

    由于单位使用了飞秋,同时也使用了OA,但OA的消息系统没有飞秋方便,所以大多数人还是在用飞秋沟通.但审批等流程又在OA上,所以做了个消息接口服务器,提取OA消息自动发送到飞秋上,大大方便了工作. 正好 ...

  7. Java虚拟机性能调优相关

    一.JVM内存模型及垃圾收集算法 1.根据Java虚拟机规范,JVM将内存划分为:New(年轻代)Tenured(年老代)永久代(Perm) 其中New和Tenured属于堆内存,堆内存会从JVM启动 ...

  8. SEO 第三章

    SEO第三章 本次课目标: 1.  掌握关键词的选取方法 2.  掌握关键词的竞争强度分析 3.  掌握关键词的拓展方法 一.关键词的选取 选择关键词的时候可以根据公司网站的定位,围绕公司的主营产品或 ...

  9. 主席树-指针实现-找第k小数

    主席树,其实就是N颗线段树 只是他们公用了一部分节点(๑•̀ㅂ•́)و✧ 我大部分的代码是从一位大佬的那里看到的 我这个垃圾程序连Poj2104上的数据都过不了TLE so希望神犇能给我看看, 顺便给 ...

  10. 【转】C#的版本

    这年头啥东东都喜欢过段时间整个啥新版本出来.汽车,手机如此,软件就更是如此了啊.比如啥Iphone 4,Iphone 5,Windows 8,Oracle 12C,SQL Server 2010. 版 ...