排序2 Insert or Merge

该文描述了一个编程问题,要求根据给定的初始无序数组和部分排序后的数组,判断使用的是插入排序还是归并排序。通过实现两种排序算法并比较它们的中间过程,来确定哪种排序方法被使用,并继续进行一次排序操作,输出最终结果。
摘要由CSDN通过智能技术生成

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

简单来说,给定一个无序数组,然后再给一组在(插入或归并)排序过程中,某序列

根据这两个数组判断是插入排序还是归并排序。

思路是:每执行一次,就判断一次,找一个标记flag,成功,再循环一次,看flag的值,结果循环。

tips:pta不给测试数据真的烦,当时卡在当N=1时,的判断,按理来说,两者都有可能。就比如

1
1 
1

借鉴别人的答案,正确输出结果是1。没有insert sort也没有merge sort

最终代码附上

#include <iostream>
#include <cstdlib>
using namespace std;
bool Judge(int A[], int Temp[], int N)
{
    for (int i = 0; i < N; i++)
    {
        if (A[i] != Temp[i])
            return 0;
    }
    return 1;
}
bool insertSort(int A[], int Temp[], int N)
{
    int i, p, temp, flag = 0;

    for (p = 1; p < N; p++)
    {
        temp = A[p];
        for (i = p; i >= 0 && temp < A[i - 1]; i--)
        {
            A[i] = A[i - 1];
        }
        A[i] = temp;
        if (flag == 1)
            return true;
        if (Judge(A, Temp, N))
            flag = 1;
    }
    return false;
}

void Merge(int A[], int Temp1[], int L, int R, int RightEnd)
{
    int LeftEnd = R - 1, i = L, Num = RightEnd - L + 1;
    while (L <= LeftEnd && R <= RightEnd)
    {
        if (A[L] < A[R])
        {
            Temp1[i++] = A[L++];
        }
        else
        {
            Temp1[i++] = A[R++];
        }
    }
    while (L <= LeftEnd)
    {
        Temp1[i++] = A[L++];
    }
    while (R <= RightEnd)
    {
        Temp1[i++] = A[R++];
    }
    for (i = 0; i < Num; i++, RightEnd--)
    {
        A[RightEnd] = Temp1[RightEnd];
    }
}
void MergePass(int A[], int Temp1[], int N, int Length)
{
    int i, j;
    for (i = 0; i <= N - 2 * Length; i += 2 * Length)
    {
        Merge(A, Temp1, i, i + Length, i + 2 * Length - 1);
    }
    if (i + Length < N)
    {
        Merge(A, Temp1, i, i + Length, N - 1);
    }
    else
    {
        for (; i < N; i++)
            Temp1[i] = A[i];
    }
}
bool MergeSort(int A[], int Temp[], int N)
{
    int flag = 0;
    int Length = 1;
    int *Temp1 = (int *)malloc(N * sizeof(int));
    if (Temp1)
    {
        while (Length < N)
        {
            MergePass(A, Temp1, N, Length);
            Length *= 2;
            if (flag == 1)
            {
                break;
            }
            if (Judge(A, Temp, N))
            {
                flag = 1;
            }
        }
        free(Temp1);
        if (flag == 1)
        {
            return true;
        }
    }
    else
        cout << "error";
    return false;
}

int main()
{

    int N;
    cin >> N;
    int A[N], B[N], C[N];
    for (int i = 0; i < N; i++)
    {
        cin >> A[i];
    }
    for (int i = 0; i < N; i++)
    {
        cin >> B[i];
    }
    for (int i = 0; i < N; i++)
    {
        C[i] = A[i];
    }
    if (insertSort(A, B, N))
    {
        cout << "Insertion Sort" << endl;
        cout << A[0];
        for (int i = 1; i < N; i++)
        {
            cout << " " << A[i];
        }
    }
    else if (MergeSort(C, B, N))
    {
        cout << "Merge Sort" << endl;
        cout << C[0];
        for (int i = 1; i < N; i++)
        {
            cout << " " << C[i];
        }
    }
    else
        cout << B[0];
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

牛魔王的老公

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值