PAT 甲级1089 Insert or Merge (25分)

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

解题思路:本题主要是通过模拟插入和归并排序,因为数据量比较小所以用sort函数就可以了。

  • 先通过模拟判断是否是插入排序,若是找出排序了几次后再排序一次。通过以下方式模拟每一次的插入排序:
sort(temp,temp+i);

若是归并排序,通过以下方式模拟每一次的归并排序:但是还是有坑需要注意。
例如数据:

for(step = 2;step / 2 < N;step*=2)
	for(i = 0;i < N;i+=step)
		sort(temp1+i,temp1+min(N,i+step));

input:

4
3 4 2 1
3 4 2 1

output:

Insertion Sort
2 3 4 1
#include<cstdio>
#include<algorithm>
using namespace std;
int  insertsort(int temp[],int b[],int N)
{
	int flag = 0,j;
	for(int i = 1;i < N;i++)
	{
		sort(temp,temp+i);
		for(j = 0;j < N;j++)
		{
			if(temp[j] != b[j])
				break;		
		}
		if(j == N)
			flag = i;
	}
	return flag;
}
void mergesort(int temp1[],int b[],int N)
{
	int step,i,j;
	for(step = 2;step / 2 < N;step*=2)
	{
		for(i = 0;i < N;i+=step)
			sort(temp1+i,temp1+min(N,i+step));
		for(j = 0;j < N;j++)
		{
			if(temp1[j] != b[j])
				break;
		}			
		if(j == N)
		{
			for(i = 0;i < N;i+=step*2)
				sort(b+i,b+min(N,i+step*2));
			break;
		}	
	}
}
int main(void)
{
	int N,a[1000],b[1000],temp[1000],temp1[1000],flag;
	scanf("%d",&N);
	for(int i = 0;i < N;i++)
	{
		scanf("%d",&a[i]);	
		temp[i] = a[i];
		temp1[i] = a[i];
	}
	for(int i = 0;i < N;i++)
		scanf("%d",&b[i]);
	flag = insertsort(temp,b,N);
	if(flag != 0)
	{
		sort(b,b+flag+1);
		printf("Insertion Sort\n");
	}
	else
	{
		mergesort(temp1,b,N);
		printf("Merge Sort\n");
	}
	for(int i = 0;i < N;i++)
		{
			printf("%d",b[i]);	
			if(i < N - 1)
				printf(" ");
		}
	return 0;
}

看了柳神的解法后重新写了一遍,代码量只有原来的一半。
首先通过遍历数组b找出第一个b[i]<=b[i+1]的下标i,若下标i之后所有的a[j] == b[j]那么是插入排序否则是归并排序。

                                             
#include<cstdio> 
#include<algorithm>
using namespace std;
int main(void)
{
   int N,a[200],b[200],i,j;
   scanf("%d",&N);
   for(i = 0;i < N;i++)
   	scanf("%d",&a[i]);
   for(i = 0;i < N;i++)
   	scanf("%d",&b[i]);
   for(i = 0;b[i] <= b[i+1] && i < N - 1;i++); 
   for(j = i + 1;j < N &&a[j] == b[j];j++);
   if(j == N)
   {
   	printf("Insertion Sort\n");
   	sort(a,a+i+2);
   }
   else
   {	
   	printf("Merge Sort\n");
   	int flag = 1;
   	while(flag)
   	{
   		int step;
   		for(step = 2;step / 2 < N;step*=2)
   		{
   			for(i = 0;i < N;i+=step)
   				sort(a+i,a+min(N,i+step));
   			for(j = 0;j < N&&a[j]==b[j];j++);
   			if(j == N)
   			{
   				flag = 0;		
   				break;
   			}
   		}
   		for(i = 0;i < N;i+=step*2)
   			sort(a+i,a+min(N,i+step*2));
   	}
   }
   for(int i = 0;i < N;i++)
   {
   	printf("%d",a[i]);
   	if(i!=N-1)
   	printf(" ");
   }
   return 0;
}
发布了24 篇原创文章 · 获赞 1 · 访问量 508

猜你喜欢

转载自blog.csdn.net/lovingcgq/article/details/104251133