1089 Insert or Merge (25分)

baixiaofei567發表於2020-11-02

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<cstdio>
#include<algorithm>
using namespace std;
const int N = 111;
int origin[N], tempOri[N], changed[N];//原始陣列,原始陣列備份,目標陣列
int n;//元素個數

bool isSame(int A[], int B[]){//判斷陣列A和陣列B是否相同
    for(int i = 0; i < n; i++){
        if(A[i] != B[i]) return false;
    }
    return true;
}

bool showArray(int A[]){
    for(int i = 0; i < n; i++){
        if(i!=0) printf(" ");
        printf("%d",A[i]);
    }
    printf("\n");
}

bool insertSort(){//插入排序
    bool flag = false;//如果有相同的就直接返回true
    //預設第一位是有序的,所以從第一位開始排序,每次將一位數字插入前面的序列中
    for(int i = 1 ; i < n; i++){
        if(i != 1 && isSame(tempOri, changed)){
            flag = true;//中間步驟與目標相同,且不是初始序列
        }
        int temp = tempOri[i], j = i;//從當前數字的前面一位開始比,如果比當前數字大,證明還要向前,大的那一位往後移,給它空一個位置
        while(j > 0 && tempOri[j - 1] > temp){
            tempOri[j] = tempOri[j - 1];
            j--;
        }
        tempOri[j] = temp;//如果找到j-1號位比temp小,證明找到了,把它插在j-1的後面一位
        if(flag == true){
            return true;
        }
    }
    return false;//歸併排序完成還沒找到
}

void mergeSort(){//歸併排序
    bool flag = false;//記錄是否存在陣列中間步驟與changed陣列相同
    //以下為歸併排序部分
    for(int step = 2; step / 2<= n; step *= 2){
        if(step != 2 && isSame(tempOri, changed)){
            flag = true;
        }
        for(int i = 0; i < n; i += step){
            sort(tempOri + i, tempOri + min(n, i +step));
        }
        if(flag == true){
            showArray(tempOri);
            return;
        }
    }
}

int main(){
    scanf("%d", &n);
    for(int i = 0; i < n; i++){
        scanf("%d", &origin[i]);//輸入起始陣列
        tempOri[i] = origin[i];//tempOri陣列為備份,排序過程在tempOri上進行
    }
    for(int i = 0; i < n; i++){
        scanf("%d", &changed[i]);//目標陣列
    }
    if(insertSort()){//如果在插入排序中找到目標陣列
        printf("Insertion Sort\n");
        showArray(tempOri);
    }
    else{//到達此處時一定是歸併排序
        printf("Merge Sort\n");
        for(int i = 0; i < n; i++){
            tempOri[i] = origin[i];//還原tempOri陣列
        }
        mergeSort();//歸併排序
    }
    return 0;
}

相關文章