中国大学MOOC-陈越、何钦铭-数据结构 Insert or Merge

来源:互联网 发布:linux kernel启动流程 编辑:程序博客网 时间:2024/05/22 11:38

题目描述:
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

思路:
这道题主要是考察插入和归并排序。不过有意思的是,这道题并没有直接让你对数进行排序,而是接着输入另一组未完全排序的数据,让你判断这是上面的哪一种排序方式产生的,并且还要你用该排序方式对此数据再多进行一次排序。对于这道题,我把首先输入,需要排序的数据称为数组A,对于部分已排序的数据称为数组B。我的思路是,写一个Check函数,用于检查B和当前正在排序的数组A是否匹配。如果匹配的话,就说明B就是当前排序方式产生的结果。所以不论我们不仅要执行插入排序,还要执行归并排序,并且在每次排序时都要执行Check函数判断。另外,这里我对插入排序,归并排序进行了改动,使得它们返回一个bool值,用于判断B是否是它产生的。

代码实现:

#include<iostream>using namespace std;#define MaxSize 100//定义数组的最大容量void Input(int A[],int N){//输入函数    for (int i = 0; i < N; i++){        cin >> A[i];    }}int Check(int A[], int B[], int N){//遍历两个数组,找出他们元素相等的个数    int total=0;//记录元素相等的个数    for (int i = 0; i < N; i++){        if (A[i] == B[i]){            total++;        }    }    return total;}bool InsertionSort(int A[], int B[], int N){//插入排序算法.A为待排序的数组,B为部分排序的数组    int i, j, total;    int temp;    for (i = 1; i < N; i++){        temp = A[i];        for (j = i; j>0 && A[j - 1] > temp; j--){            A[j] = A[j - 1];        }    A[j] = temp;    total = Check(A, B, N);    if (total == N){//判断是否是插入排序        break;    }    }    if (total == N){//如果是插入排序        i++; temp = A[i];//接下来就是多做一次插入排序        for (j = i; j > 0 && A[j - 1] > temp; j--){            A[j] = A[j - 1];        }        A[j] = temp;        return true;//返回真    }    else return false;//否则返回假}void Merge(int A[], int Temp[], int Lpos, int Rpos,int RightEnd){//归并两个较小的数组    int i, Tpos, LeftEnd, Num;    LeftEnd = Rpos - 1;    Num = RightEnd - Lpos + 1;    Tpos = Lpos;    while (Lpos <= LeftEnd&&Rpos <= RightEnd){        if (A[Lpos] <= A[Rpos]){            Temp[Tpos++] = A[Lpos++];        }        else {            Temp[Tpos++] = A[Rpos++];        }    }    while (Lpos <= LeftEnd){ Temp[Tpos++] = A[Lpos++]; }    while (Rpos <= RightEnd){ Temp[Tpos++] = A[Rpos++]; }    for (i = 0; i < Num; i++,RightEnd--){        A[RightEnd] = Temp[RightEnd];    }}void Merge_pass(int A[], int Temp[], int N,int Length){//循环版归并排序实现,Length为当前子序列的长度    int i, j;    for (i = 0; i <= N - 2 * Length; i += 2 * Length){        Merge(A, Temp, i, i + Length, i + 2 * Length - 1);    }    if (i + Length < N){//归并最后两个子列        Merge(A, Temp, i, i + Length, N - 1);    }    else {//处理最后一个子列        for (j = i; j < N; j++){            Temp[j] = A[j];        }    }}bool MergeSort(int A[],int B[], int N){//归并排序算法实现    int *Temp;    int Length,total;    Temp = new int[N];    Length = 1;//子列最初长度为1    while (Length < N){        Merge_pass(A, Temp, N, Length);        Length *= 2;        for (int i = 0; i < N; i++){//把元素复制回来            A[i] = Temp[i];        }        total = Check(A, B, N);        if (total == N){ break; }    }        if (total == N){//如果是归并排序            Merge_pass(A, Temp, N, Length);//就多做一次归并排序            for (int i = 0; i < N; i++){//把元素复制回来                A[i] = Temp[i];        }            return true;    }    else {        return false;    }    delete Temp;    Temp = NULL;}void Output(int A[],int N){//输出函数    for (int i = 0; i < N - 1; i++){        cout << A[i] << " ";    }    cout << A[N - 1];}int main(){    int N,i,key;    bool j1,j2;    int A1[MaxSize], A2[MaxSize];//归并排序和插入排序各用一个相同的数组    int B[MaxSize];//储存部分已排序的数    cin >> N;    for (i = 0; i < N; i++){        cin >> key;        A1[i] = A2[i] = key;    }    for (i = 0; i < N; i++){//输入部分已排序的数组        cin >> B[i];    }    j1 = InsertionSort(A1, B, N);    j2 = MergeSort(A2, B, N);    if (j1){        cout << "Insertion Sort" << endl;        Output(A1, N);    }    if(j2){        cout << "Merge Sort" << endl;        Output(A2, N);    }}
0 0
原创粉丝点击