PAT 数据结构 07-排序2. Insert or Merge (25)
来源:互联网 发布:link域名可以备案吗 编辑:程序博客网 时间:2024/05/16 06:44
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
对第一个数组进行2、4、8...归排,看是否能得到第二个数组,得到则进行下一步归排。
若得不到则对第二个数组进行直接插入排序。
/*2015.7.13cyq*/#include <iostream>#include <vector>using namespace std;void merge(vector<int> &a,int begin1,int end1,int begin2,int end2){int n=end2-begin1+1;vector<int> b(n);int i1=begin1;int i2=begin2;int i=0;while(i1<=end1&&i2<=end2){if(a[i1]<=a[i2])b[i++]=a[i1++];elseb[i++]=a[i2++];}if(i1<=end1)while(i<n)b[i++]=a[i1++];if(i2<=end2)while(i<n)b[i++]=a[i2++];i=begin1;int j=0;while(j<n)a[i++]=b[j++];}void mergeSort(vector<int> &a,int count){//目标是每count个为一组int len=a.size();int i=0;while(i<len){if(i+count-1<len){//两个子数组加起来有count个merge(a,i,i+count/2-1,i+count/2,i+count-1);}else{if(i+count/2<len)//存在两个子数组,后一个残缺merge(a,i,i+count/2-1,i+count/2,len-1);}i=i+count;}}int main(){int N;cin>>N;vector<int> a(N),b(N);for(int i=0;i<N;i++)cin>>a[i];for(int i=0;i<N;i++)cin>>b[i];vector<int> tmp(a);int count=1;while(count<N){count*=2;mergeSort(tmp,count);if(tmp==b){mergeSort(tmp,count*2);cout<<"Merge Sort"<<endl;cout<<tmp[0];for(int i=1;i<N;i++)cout<<" "<<tmp[i];return 0;}}cout<<"Insertion Sort"<<endl;int index=N;for(int i=1;i<N;i++){if(b[i]<b[i-1]){index=i;break;}}if(index<N){<span style="white-space:pre"></span>int t=b[index];<span style="white-space:pre"></span>int j=index-1;<span style="white-space:pre"></span>while(j>=0&&b[j]>t){<span style="white-space:pre"></span>b[j+1]=b[j];<span style="white-space:pre"></span>j--;<span style="white-space:pre"></span>}<span style="white-space:pre"></span>b[j+1]=t;<span style="white-space:pre"></span>}cout<<b[0];for(int i=1;i<N;i++)cout<<" "<<b[i];return 0;}
- PAT 数据结构 07-排序2. Insert or Merge (25)
- 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)
- PAT 1089. Insert or Merge (25)
- PAT A 1089. Insert or Merge (25)
- PAT 1089. Insert or Merge (25)
- PAT(Advance) 1089. Insert or Merge (25)
- PAT 1089. Insert or Merge (25)
- 【PAT】1089. Insert or Merge (25)
- PAT-A1089. Insert or Merge (25)
- PAT 1061Insert or Merge (25)
- PAT(A) - 1089. Insert or Merge (25)
- pat-a1089. Insert or Merge (25)
- PAT A1089 insert or merge (25)
- PAT-A 1089. Insert or Merge (25)
- 【leetcode-90】SubsetII(java 语言)
- CPaintDC 、CWindowDC、 CClientDC、 CDC的区别与联系
- 虚函数、虚指针和虚表
- 简单加解密
- 算法导论 第四章:分治法(二)
- PAT 数据结构 07-排序2. Insert or Merge (25)
- opcache配置
- 每位Android开发人员不容错过的十大实用工具
- N-Queens II 回溯法求八皇后
- C语言中关键字auto、static、register、const、volatile、extern的作用
- POJ 3286 How many 0's?(数位DP)
- NYOJ 519
- 为码农正名——在互联网公司工作啥样子
- pandas重写Q4