07-排序2. Insert or Merge (25)
来源:互联网 发布:ubuntu怎么使用putty 编辑:程序博客网 时间:2024/05/17 21:18
时间限制
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 resulting 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:103 1 2 8 7 5 9 4 6 01 2 3 7 8 5 9 4 6 0Sample Output 1:
Insertion Sort1 2 3 5 7 8 9 4 6 0Sample Input 2:
103 1 2 8 7 5 9 4 0 61 3 2 8 5 7 4 9 0 6Sample Output 2:
Merge Sort1 2 3 8 4 5 7 9 0 6
#include <iostream>#define N 101using namespace std;typedef int ElemType;void print(int a[], int n){ for(int j= 0; j<n-1; j++){ cout<<a[j] <<" "; } cout<<a[n-1]<<endl;}bool InsertSort(int a[], int n,int c[]){ bool ppp,result=false; int i,j,flag,tmp,qqq=0; for(i=1;i<n;i++) { ppp = true; tmp = i-1; flag=a[i]; while(tmp>=0&&a[tmp]>flag) { a[tmp+1]=a[tmp]; tmp--; } a[++tmp]=flag; for(j=0;j<n;j++) { if(a[j]!=c[j]) { ppp=false; break; } } if(qqq==1) { cout<<"Insertion Sort\n"; print(a,n); result=true; break; } if(ppp) qqq=1; } return result;}//将r[i…m]和r[m +1 …n]归并到辅助数组rf[i…n]void Merge(ElemType *r,ElemType *rf, int i, int m, int n){ int j,k; for(j=m+1,k=i; i<=m && j <=n ; ++k){ if(r[j] < r[i]) rf[k] = r[j++]; else rf[k] = r[i++]; } while(i <= m) rf[k++] = r[i++]; while(j <= n) rf[k++] = r[j++]; //print(rf,n+1);}bool MergeSort(ElemType *r, ElemType *rf, int lenght,int c[]){ int len = 1,qqq=0; bool ppp,result=false; ElemType *q = r ; ElemType *tmp ; while(len < lenght) { ppp=true; int s = len; len = 2 * s ; int i = 0; while(i+ len <lenght){ Merge(q, rf, i, i+ s-1, i+ len-1 ); //对等长的两个子表合并 i = i+ len; } if(i + s < lenght){ Merge(q, rf, i, i+ s -1, lenght -1); //对不等长的两个子表合并 } tmp = q; q = rf; rf = tmp; //交换q,rf,以保证下一趟归并时,仍从q 归并到rf if(qqq==1) { cout<<"Merge Sort\n"; print(r,lenght); result=true; break; } //print(rf,lenght); for(int j=0;j<lenght;j++) if(rf[j]!=c[j]) { ppp=false; break; } if(ppp) qqq=1; } return result;}int main(){ int n,a[N],i,b[N],c[N],d[N]; cin>>n; for(i=0;i<n;i++) { cin>>a[i]; d[i]=a[i]; } for(i=0;i<n;i++) { cin>>c[i]; } if(InsertSort(a,n,c)); else MergeSort(d,b,n,c); //print(a,n); return 0;}
- 07-排序2. Insert or Merge (25)
- 07-排序2. Insert or Merge (25)
- 09-排序2. Insert or Merge (25)
- PAT 数据结构 07-排序2. Insert or Merge (25)
- 07-2. Insert or Merge (25)
- PAT 1089. Insert or Merge (25) 插入排序+归并排序
- PAT 1088 1089. Insert or Merge (25)(排序啊)
- 09-排序2 Insert or Merge (25分)
- 09-排序2 Insert or Merge (25分)
- 1089. Insert or Merge -25[插入和归并排序]
- 09-排序2 Insert or Merge (25分)
- 09-排序2 Insert or Merge (25分)
- 1089. Insert or Merge (25)
- 1089. Insert or Merge (25)
- 1089. Insert or Merge (25)
- 1089. Insert or Merge (25)
- 1089. Insert or Merge (25)
- 1089. Insert or Merge (25)
- 我的第一个博客
- LeetCode:Same Tree
- android---xml---include用法(导入另一个XML文件到本布局中)
- 遇见SwipeRefreshLayout一点点问题,设置setRefreshing(true)没效果
- 【正途】Android R.java文件丢失的问题
- 07-排序2. Insert or Merge (25)
- urlurl
- java——获取一个应用程序运行的次数,如果超过5次,给出使用次数已到请注册的提示,并不要再运行程序
- 磁盘空间检查
- setContentView( )方法
- jquery监听回车键自动提交
- 解决 Errors running builder 'DeploymentBuilder' on project ' .
- Create a Bundle Jar With Ant:使用Ant创建一个包含所有依赖的JAR包
- TCP带外数据学习总结(概念,发送接收过程,数据到达检测,代码实现)