7-12 插入排序还是堆排序 (25 分)

根据维基百科的定义:

  • 插入排序是迭代算法,逐一获得输入数据,逐步产生有序的输出序列。每步迭代中,算法从输入序列中取出一元素,将之插入有序序列中正确的位置。如此迭代直到全部元素有序。
  • 堆排序也是将输入分为有序和无序两部分,迭代地从无序部分找出最大元素放入有序部分。它利用了大根堆的堆顶元素最大这一特征,使得在当前无序区中选取最大元素变得简单。

  现给定原始序列和由某排序算法产生的中间序列,请你判断该算法究竟是哪种排序算法?

输入格式:

  输入在第一行给出正整数 N (≤100);随后一行给出原始序列的 N 个整数;最后一行给出由某排序算法产生的中间序列。这里假设排序的目标序列是升序。数字间以空格分隔。

输出格式:

  首先在第 1 行中输出Insertion Sort表示插入排序、或Heap Sort表示堆排序;然后在第 2 行中输出用该排序算法再迭代一轮的结果序列。题目保证每组测试的结果是唯一的。数字间以空格分隔,且行首尾不得有多余空格。

输入样例 1:

10
3 1 2 8 7 5 9 4 6 0
1 2 3 7 8 5 9 4 6 0

输出样例 1:

Insertion Sort
1 2 3 5 7 8 9 4 6 0

输入样例 2:

10
3 1 2 8 7 5 9 4 6 0
6 4 5 1 0 3 2 7 8 9

输出样例 2:

Heap Sort
5 4 3 1 0 2 6 7 8 9

Code

#include<bits/stdc++.h>
#define ll long long
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
const ll MAXN = 1e12 + 10;
ll n, m, i, j, k, t, w, x, y, z, in, flag, cou, sum, maxn, minn;
string s, s1, s2;
const ll INF = 1e9 + 7;
void InsertSort ( ll *temp, ll len, ll n )
{
    if ( temp[len] > temp[len - 1] || len > n ) return;

    temp[0] = temp[len];
    temp[len] = temp[len - 1];
    ll i;

    for ( i = len - 2; temp[0] < temp[i]; i-- )
        temp[i + 1] = temp[i];

    temp[i + 1] = temp[0];
}

void PerHeapSort ( ll *temp, ll i, ll n )
{
    ll t = temp[i], child;

    while ( 2 * i <= n )
    {
        child = i * 2;

        if ( temp[child] < temp[child + 1] && child + 1 <= n ) child++;

        if ( t < temp[child] ) temp[i] = temp[child];
        else break;

        i = child;
    }

    temp[i] = t;
}
void HeapSort ( ll *temp, ll n )
{
    for ( ll i = n / 2; i >= 1; i-- )
        PerHeapSort ( temp, i, n );


}
bool isEqual ( ll *a, ll *b, ll n )
{
    for ( ll i = 1; i <= n; i++ )
        if ( a[i] != b[i] ) return 0;

    return 1;
}
void ShowSeq ( ll *a, ll n )
{
    cout << a[1];

    for ( ll i = 2; i <= n; i++ ) cout << " " << a[i];

    cout << endl;
}
int main()
{
    FAST_IO;
    cin >> n;
    ll initseq[n + 1], halfsortseq[n + 1], temp1[n + 1], temp2[n + 1];

    for ( i = flag = 1; i <= n; i++ ) cin >> initseq[i], temp1[i] = temp2[i] = initseq[i];

    for ( i = 1; i <= n; i++ ) cin >> halfsortseq[i];

    for ( i = 2; i <= n; i++ )
    {
        InsertSort ( temp1, i, n );

        if ( isEqual ( temp1, halfsortseq, n ) )
        {
            cout<<"Insertion Sort"<<endl;
            InsertSort ( temp1, i + 1, n );
            ShowSeq ( temp1, n );
            flag = 0;
            break;
        }
    }

    for ( i = n; flag && i >= 1; i-- )
    {
        if(i==n)HeapSort(temp2,i);
        else PerHeapSort(temp2,1,i);

        if ( isEqual ( temp2, halfsortseq, n ) )
        {
             cout<<"Heap Sort"<<endl;
            swap(temp2[1],temp2[i]);
            PerHeapSort(temp2,1,i-1);
            ShowSeq ( temp2, n );
            break;
        }
        swap(temp2[1],temp2[i]);
    }

    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

GUESSERR

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

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

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

打赏作者

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

抵扣说明:

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

余额充值