您的位置:首页 > 理论基础 > 数据结构算法

PAT 数据结构 07-排序2. Insert or Merge (25)

2015-07-13 19:06 531 查看
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:
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

对第一个数组进行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++];
else
b[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;
}

[/code]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: