1089 Insert or Merge (25 分)

1089 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

题意

        给出一个初始序列,可以将它使用插入排序或归并排序进行排序。现在给出一个序列,问它是由插入排序还是归并排序产生的,并输出下一步将会产生的序列。

代码

参考大佬代码而写o(╥﹏╥)o

#include<bits/stdc++.h>

using namespace std;

int a[105];
int b[105];
int n;

int main(){
    
    
	cin >> n;
	
	int i,j,step=1;
	for(i=0; i<n; i++) {
    
    
		cin >> a[i];
	}
	
	for(i=0; i<n; i++) {
    
    
		cin >> b[i];
	}
	
	// 以下两个for循环判断其是不是插入排序,如果是则j==n
	for (i = 0; i < n-1 ; i++) {
    
    
		if(b[i] > b[i+1]) {
    
    
			break;
		}
	}
    for (j = i+1; j<n; j++) {
    
    
    	if(a[j] != b[j]) {
    
    
    		break;
		}
	}
    
    if(j == n) {
    
    
    	cout << "Insertion Sort" << endl;
        sort(a, a+(i+1)+1);
	} else {
    
    
		cout << "Merge Sort" << endl;
		bool flag = true;
		while(flag) {
    
    
			flag = false;
			for(i=0; i<n; i++) {
    
      // 判断a序列排序的和b序列是否一样
				if(a[i] != b[i]) {
    
    
					flag = true;
				}
			}
			step *= 2;
			// 将a序列排序到和b序列一样,然后多排一次即可输出结果
			for(int i=0; i<n/step; i++) {
    
    
				sort(a+i*step,a+(i+1)*step); // 两两排序
			}
			sort(a+(n/step)*step, a+n); // 如果n/step不是整除,则将后面的也排序 
		}
	}
	
	for (i = 0; i < n-1; i++) {
    
    
        cout << a[i] << " ";
    }
    cout << a[n-1];
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_44635198/article/details/114378247
今日推荐