1089

#include 
#include 
#include 
using namespace std;
int n;
int origin[110],change[110],temp[110]; 
bool issame(int a[],int b[])
{
	for(int i = 0; i < n; i++)
	{
		if(a[i] != b[i])
		return false;
 	}
 	return true;
}
bool isinsert()
{
	bool flag = false;
	for(int i = 0; i < n-1; i++)
	{
		sort(origin,origin + i + 2);
		if(flag == true)
		{   printf("Insertion Sort\n");
			for(int j = 0; j < n; j ++)
			{
				if(j != 0) printf(" ");
				printf("%d",origin[j]);
			}
			return true;
		}
		
		if(issame(origin,change) == true) 
		flag = true;
	}
	return false;
}
void merge()
{
	int flag = 0;
	for(int step = 2; step <= n;step *= 2)
	{
		for(int i = 0; i < n; i += step)
		{
			sort(temp+i,temp+min(n,i + step));
		}
		if(flag == 1) 
		{
			for(int j = 0; j < n; j++)
			{
				if(j != 0) printf(" ");
				printf("%d",temp[j]);
			}
				return ;
		}
		if(issame(temp,change) == true)
		{
			flag = 1;
		}
		
	}
	
}
int main()
{
	scanf("%d",&n);
	for(int i = 0; i < n; i++)
	{
		scanf("%d",&origin[i]);
	}
	for(int i = 0; i < n; i++)
	{
		scanf("%d",&change[i]);
	}
	for(int i = 0; i < n; i++)
	{
		temp[i] = origin[i];
	}
	if (isinsert())
	return 0;
	printf("Merge Sort\n");
	merge();
return 0;
}

 

你可能感兴趣的:(1089)