PTA 09-排序2 Insert or Merge(25 分)

题目:

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

胡乱分析:

分析不动了,发现自己越来越菜了.jpg

代码:

#include 
#include 
using namespace std;
int main() {
    int N;
    cin >> N;
    int *a = new int[N];
    int *b = new int[N];
    for (int i = 0; i < N; i++) {
        cin >> a[i];
    }
    for (int i = 0; i < N; i++) {
        cin >> b[i];
    }
    int index;
    for (index = 1; b[index] >= b[index - 1] && index < N; index++);
    //这里一定要是大于等于啊!!!
    int count = index;
    while (count < N && a[count] == b[count]) {
        count++;
    }
    if (count == N) {
        cout << "Insertion Sort\n";
        sort(b, b + index + 1);
        cout << b[0];
        for (int i = 1; i < N; i++) {
            cout << " " << b[i];
        }
    }
    else {
        cout << "Merge Sort\n";
        int length, flag = 1;//从2开始找数组b的归并长度
        for (length = 2; flag; length *= 2) {
            for (int i = length; i < N; i += 2 * length) {
                //如果从length开始相距length*2的所有接口处都有序//则length*=2
                if (b[i - 1] > b[i]) {
                    flag = 0; break;//接口处无序,跳出循环
                }
            }
        }
        cout << length;
        //注意for循环中length是*2再判断flag的,因此跳出循环后的length就是下次要归并的长度
        int i;
        for (i = 0; i < N - length; i += length) {
            sort(b + i, b + i + length);//按照长度逐次归并
        }
        sort(b + i, b + N);//归并剩余部分
        cout << b[0];
        for (int i = 1; i < N; i++) {
            cout << " " << b[i];
        }
    }
}

//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


//10
//3 1 2 8 7 5 9 4 0 6
//1 3 2 8 5 7 4 9 0 6
//
//Merge Sort
//1 2 3 8 4 5 7 9 0 6

你可能感兴趣的:(PAT数据结构,数据结构)