1. 程式人生 > >《資料結構》09-排序2 Insert or Merge

《資料結構》09-排序2 Insert or Merge

分析

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:

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

分析

題目大意就是給出一組初始數和一組排序中間過程的數,判斷中間過程的這組數是由插入排序還是歸併排序得來的
基本思路是:實現歸併排序和插入排序,排一次比較一次,如果排序數和中間過程的數相等了,再做一輪後輸出

#include<iostream>
using namespace std; 

bool judge(int A[],int B[],int N){
   for(int i=0;i<N;i++)
   	if(A[i]!=B[i])
   		return false;
   return true;
}

// 插入排序 
bool Insertion_Sort(int A[],int B[],int N){
   for(int P=1;P<N;P++){
   	int tmp = A[P];
   	int j = P;
   	for(;j>0 && tmp < A[j-1];j--)
   		A[j] = A[j-1];
   	A[j] = tmp;
   	if(judge(A,B,N)){  // 如果相等了,再做一輪 
   		P++;
   		int tmp = A[P];
   		int j = P;
   		for(;j>0 && tmp < A[j-1];j--)
   			A[j] = A[j-1];
   		A[j] = tmp;
   		return false;
   	}
   }
   return true; 
}


void Merge(int A[],int tmpA[],int L,int R,int RightEnd){
   // L = 左邊起始位置,R = 右邊起始位置,RightEnd = 右邊終點位置 
   int NumSize = RightEnd-L+1;   //  排序個數  
   int LeftEnd = R-1;   // 左邊終止位置 
   int tmp = L; 
   // 排序 
   while(L <= LeftEnd && R <= RightEnd){
   	if(A[L] <= A[R])
   		tmpA[tmp++] = A[L++];
   	else
   		tmpA[tmp++] = A[R++];
   } 
   // 如果左邊有剩 
   while(L <= LeftEnd)
   	tmpA[tmp++] = A[L++];
   // 如果右邊有剩 
   while(R <= RightEnd)
   	tmpA[tmp++] = A[R++];
   //  導回 A 陣列 
   for(int i=0;i<NumSize;i++)
   	A[RightEnd--] = tmpA[--tmp];
}

void Merge_pass(int A[],int tmpA[],int N,int length){
   int i;
   // 每次 2*length 為一組排序單元 
   for(i=0;i<=N-2*length;i+=2*length)
   	Merge(A,tmpA,i,i+length,i+length*2-1);
   // 處理剩下的不夠一組的排序單元 
   if(i+length < N)   // 如果左邊夠了,但是右邊不齊,再次進入排序 
   	Merge(A,tmpA,i,i+length,N-1);
   else   // 如果左邊都不夠,直接導給 tmpA 
   	for(int j=i;j<N;j++)
   		tmpA[j] = A[j];			
}

// 歸併排序
bool Merge_Sort(int A[],int B[],int N){
   int tmpA[N];
   int length = 1;
   while(length < N){
   	Merge_pass(A,tmpA,N,length);  // 一趟歸併 
   	length *=2;
   	if(judge(A,B,N)){   // 如果相等了,再做一輪 
   		Merge_pass(A,tmpA,N,length);
   		return false;
   	}
   } 
   return true;
}

// 輸出 
void output(int A[],int N){
   for(int i=0;i<N;i++){
   	if(i)
   		cout<<" ";
   	cout<<A[i]; 
   } 
} 

int main(){
   int N;
   cin>>N;
   int A[N],tmpA[N];
   for(int i=0;i<N;i++){
   	cin>>A[i];
   	tmpA[i] = A[i];
   }
   int B[N];
   for(int i=0;i<N;i++)
   	cin>>B[i];
   // 用 tmpA 陣列做歸併排序,判斷是否是歸併排序 
   if(!Merge_Sort(tmpA,B,N)){
   	cout<<"Merge Sort"<<endl;
   	output(tmpA,N);
   	return 0;
   }
   // 用 A 陣列做插入排序,判斷是否是插入排序
   if(!Insertion_Sort(A,B,N)){
   	cout<<"Insertion Sort"<<endl;
   	output(A,N);
   	return 0;
   }
   return 0;
}