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

解题思路

首先把插入排序和归并排序的代码写出来,然后在排序的每一小步之后加一个判断就行了。需要注意的是,根据第二个输入样例和输出样例,只能写迭代版的归并排序。

代码

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

#define MAXSIZE 100

void insert_sort(int A[], int N);
void merge_sort(int A[], int N);
void merge_pass(int A[], int tempA[], int N, int stride);
void merge(int A[], int tempA[], int low, int mid, int high);
bool is_same(int A[], int B[], int N);
void print(int A[], int N);

int B[MAXSIZE];

int main() {
    int N, A[MAXSIZE];
    scanf("%d", &N);
    for (int i = 0; i < N; i++)
        scanf("%d", &A[i]);
    for (int i = 0; i < N; i++)
        scanf("%d", &B[i]);

    int insert_A[MAXSIZE], merge_A[MAXSIZE];
    for (int i = 0; i < N; i++)
        insert_A[i] = merge_A[i] = A[i];

    insert_sort(insert_A, N);
    merge_sort(merge_A, N);

    return 0;
}

void insert_sort(int A[], int N) {
    int temp, i, j;
    for (i = 1; i < N; i++) {
        temp = A[i];
        for (j = i - 1; j >= 0 && A[j] > temp; j--)
            A[j + 1] = A[j];
        A[j + 1] = temp;

        if (is_same(A, B, N)) {
            printf("Insertion Sort
");
            i++; int k;
            temp = A[i];
            for (k = i - 1; k >= 0 && A[k] > temp; k--)
                A[k + 1] = A[k];
            A[k + 1] = temp;
            print(A, N);
            return;
        }
    }
}

void merge_sort(int A[], int N) {
    int *tempA = (int*) malloc(N * sizeof(int));
    int stride = 1;
    while (stride < N) {
        merge_pass(A, tempA, N, stride);
        stride *= 2;
        if (is_same(A, B, N)) {
            printf("Merge Sort
");
            merge_pass(A, tempA, N, stride);
            print(A, N);
            free(tempA);
            return;
        }
    }
    free(tempA);
}

void merge_pass(int A[], int tempA[], int N, int stride) {
    int i;
    for (i = 0; i <= N - 2 * stride; i += 2 *stride)
        merge(A, tempA, i, i + stride - 1, i + 2 * stride - 1);
    if (i + stride < N)
        merge(A, tempA, i, i + stride - 1, N - 1);
    else {}
        //什么都不做
} 

void merge(int A[], int tempA[], int low, int mid, int high) {
    int i = low, j = mid + 1, k = low;
    while (i <= mid && j <= high) {
        if (A[i] <= A[j])
            tempA[k++] = A[i++];
        else
            tempA[k++] = A[j++];
    }
    while (i <= mid)
        tempA[k++] = A[i++];
    while (j <= high)
        tempA[k++] = A[j++];
    for (int m = low; m <= high; m++)
        A[m] = tempA[m];
}

bool is_same(int A[], int B[], int N) {
    for (int i = 0; i < N; i++) {
        if (A[i] != B[i]) return false;
    }
    return true;
}

void print(int A[], int N) {
    printf("%d", A[0]);
    for (int i = 1; i < N; i++)
        printf(" %d", A[i]);
    printf("
");
}