天天看點

PAT 資料結構 07-排序2. 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 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;
}
           

繼續閱讀