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:

  1. 10
  2. 3 1 2 8 7 5 9 4 6 0
  3. 1 2 3 7 8 5 9 4 6 0

Sample Output 1:

  1. Insertion Sort
  2. 1 2 3 5 7 8 9 4 6 0

Sample Input 2:

  1. 10
  2. 3 1 2 8 7 5 9 4 0 6
  3. 1 3 2 8 5 7 4 9 0 6

Sample Output 2:

  1. Merge Sort
  2. 1 2 3 8 4 5 7 9 0 6
  1. #include<iostream>
  2. using namespace std;
  3. void Merge(int sequence1[],int l,int r,int rend,int temp[]){
  4. int lend=r-;
  5. int s=l;
  6. while(l<=lend&&r<=rend){
  7. if(sequence1[l]<sequence1[r]) temp[s++]=sequence1[l++];
  8. else temp[s++]=sequence1[r++];
  9. }
  10. while(l<=lend) temp[s++]=sequence1[l++];
  11. while(r<=rend) temp[s++]=sequence1[r++];
  12. }
  13. int main(){
  14. int N;
  15. int flag=;//Insertion_sort
  16. cin>>N;
  17. int sequence[N];
  18. int sequence1[N];
  19. for(int i=;i<N;i++)
  20. cin>>sequence[i];
  21. for(int i=;i<N;i++)
  22. cin>>sequence1[i];
  23. int i,j;
  24. for(j=;j<N-;j++)
  25. if(sequence1[j]>sequence1[j+]) break;
  26. int sign=j;
  27. for(j=j+;j<N;j++)
  28. if(sequence[j]!=sequence1[j]) flag=;//Merge_sort
  29. if(flag==){
  30. cout<<"Insertion Sort"<<endl;
  31. int temp=sequence1[sign+];
  32. for(i=sign+;i>=;i--)
  33. if(temp<sequence1[i-])
  34. sequence1[i]=sequence1[i-];
  35. else break;
  36. sequence1[i]=temp;
  37. int tag=;
  38. for( i=;i<N;i++){
  39. if(tag++==) cout<<sequence1[i];
  40. else cout<<" "<<sequence1[i];
  41. }
  42. }
  43. else{
  44. int tag=;
  45. cout<<"Merge Sort"<<endl;
  46. int length;
  47. for(length=;length<=N;length*=){
  48. for(i=length-;i<N-;i+=*length)
  49. if(sequence1[i]>sequence1[i+])
  50. {tag=; break;}
  51. if(tag==) break;
  52. }
  53. int temp[N];
  54. for(i=;i<N-*length;i+=*length)
  55. Merge(sequence1,i,i+length,i+*length-,temp);
  56. if(i+length<N)
  57. Merge(sequence1,i,i+length,N-,temp);
  58. else
  59. for(;i<N;i++) temp[i]=sequence[i];
  60. int tick=;
  61. for(int k=;k<N;k++)
  62. if(tick++==) cout<<temp[k];
  63. else cout<<" "<<temp[k];
  64. cout<<endl;
  65. }
  66. return ;
  67. }
  1.  

 

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. scanf()和scanf_s()

    在最初的C语言中,原版的输入就是scanf("<格式化字符串>",<地址表>) ANSI C中没有scanf_s(),只有scanf(),scanf()在读 ...

  2. Java | 技术应用 | 利用Jsoup处理页面

    根据微信公众号的推文链接地址,对文章内容进行爬取,利用jsoup解析文章源代码,加上结合xpth提取文文章信息, 利用正则表达式读取文章发表时间. Jsoup <!-- jsoup HTML p ...

  3. bzoj1854 [Scoi2010]游戏【构图 并查集】

    传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1854 没想到怎么做真是不应该,看到每个武器都有两个属性,应该要想到连边构图的!太不应该了! ...

  4. 用python编写的excel拆分小工具

    from datetime import date,datetime from openpyxl import Workbook from openpyxl import load_workbook ...

  5. Visual Studio Code配置 HTML 开发环境

    Visual Studio Code配置 HTML 开发环境 https://v.qq.com/x/page/l0532svf47c.html?spm=a2h0k.11417342.searchres ...

  6. MVC:html动态追加行及取值

    先一个button   id=addRow 点击事件进行添加 $("#addRow").bind("click", function () { var addH ...

  7. 《Head First HTML与CSS》的CSS属性

    关于继承的结论. 1.元素选择器的作用强于继承的作用:用户定义强于浏览器默认(详见(3)<Head First HTML与CSS>学习笔记---CSS入门的2) 2.基于类的选择器> ...

  8. php中的define()函数

    <?php define("PI",3.1415926); //定义常量 $r=12;//定义圆半径 echo "半径为12的单位的圆的面积".PI*($ ...

  9. Javascript异步编程的常用方法

    Javascript语言的执行环境是"单线程"(single thread).所谓"单线程",就是指一次只能完成一件任务.如果有多个任务,就必须排队,前面一个任 ...

  10. 【转载】SQL Server 2012 日志传送

    SQL Server 2012 日志传送 一.准备: 数据库为完全恢复模式,并事先做一次完全备份. 共享一个文件夹,主机备份放在这个文件夹,而且客户机有权访问这个共享文件夹. 二.基本配置 1.启动配 ...