A1089 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?

 

输入描述:

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.


 

输出描述:

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.

输入例子:

10

3 1 2 8 7 5 9 4 6 0

1 2 3 7 8 5 9 4 6 0

输出例子:

Insertion Sort

1 2 3 5 7 8 9 4 6 0

大意:给出一个两个数列,一个原数列,一个排序后数列,判断排序后数列是通过归并排序还是插入排序产生的,并且输出下一步将会产生的序列。

思路:1.前置条件:掌握插入排序和归并排序的非递归方法。

2.整体做法:先进行插入排序,如果执行过程中与改变的数组相同,即为插入排序,同时输出下一次的结果。程序语言描述为

if(Insertion ()) {输出下一次结果}

else {

进行归并排序,并输出结果。

}

3.插入排序过程: 通过bool flag 标记,对原数组每一次插入排序过程中,都判断是否与改变数组相同,

for (int i=0;i<n;i++){

if(i!=1&&比较(原数组,改变数组)==true),

再进行插入排序

4:归并排序,通过步长step*=2,非递归实现。(请重点掌握非递归实现归并排序方法)同样每进行一次排序,就需要进行判断。

注意点:初始序列不参与是否与目标序列相同的比较。(使用一个temp[]数组解决)。

代码:

#include<cstdio>
#include<algorithm>
using namespace std;
const int N=111;
int origin[N],temp[N],changed[N];  //origin为原数组,temp为置换过程的数组,changed为目标数组 
int n; //元素个数 
bool  isSame(int a[],int b[]){   //判断两个数列是否相等 
	for(int i=0;i<n;i++){
		if(a[i]!=b[i])return false;
	}
	return true;
}
void printArray(int a[]){
	for(int i=0;i<n;i++) {
		printf("%d",a[i]);
		if(i<n-1)printf("-");
	}
	printf("\n");
}
bool InsertSort(){
	bool flag=false;
	for(int i=0;i<n;i++){
		if(i!=1&&isSame(temp,changed)){  //置换过程中如果与目标数组相同,说明找到数组 
			flag=true;
		}
		int temp1=temp[i],j=i;
		while(temp[j-1]>temp1&&j>0){
			temp[j]=temp[j-1];
			j--;
		}
		temp[j]=temp1; 
		if(flag==true){   //找到目标数组,说明是插入排序,返回true 
			return true;
		}
	}
	return false;
}


void mergeSort(){
	bool flag=false;
	for(int step=2;step/2<n;step*=2){     //步长为2,4,8,16,归并排序的迭代算法 
		if(step!=2&&isSame(temp,changed)){
	         flag=true;            //找到了目标数组,返回true; 
		}
		for(int i=0;i<n;i+=step){
			sort(temp+i,temp+min(i+step,n));
		}
		if(flag==true){
			return;
		}
	}
}
int main(){
	scanf("%d",&n);
	for(int i=0;i<n;i++){
	    scanf("%d",&origin[i]);
	    temp[i]=origin[i];
	}
	for(int i=0;i<n;i++){
		scanf("%d",&changed[i]);
	}
	if(InsertSort()){          //此处有遗忘一个括号!!! 
		printf("Insertition Sort!\n");
		printArray(temp);
	}else {
		printf("Merge Sort\n");
		for(int i=0;i<n;i++){
			temp[i]=origin[i];
		}
		mergeSort();
		printArray(temp); 
	}
	return 0;
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值