L3-1 Insertion or Heap Sort (25 分)(c++自带的堆排序使用)

题目给你一个原数组,和一个经过排序之后的数组,但是只排到一半,没有排完,让你判断是堆排序还是插入排序,并且输出下一步排序之后的数组 。
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 6 0
6 4 5 1 0 3 2 7 8 9
Sample Output 2:
Heap Sort
5 4 3 1 0 2 6 7 8 9

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int main()
{
    int n;
    cin >> n;
    int a[n];
    int b[n];
    int c[n];
    for (int i = 0; i < n; i++)
    {
        cin >> a[i];
        c[i] = a[i];
    }
    for (int i = 0; i < n; i++)
        cin >> b[i];
    int num = 0;
    sort(c, c + n);
    int cnt = 0;
    while (b[cnt] <= b[cnt + 1])
    {
        cnt++;
    }
    if (cnt)
    {
        cout << "Insertion Sort" << endl;
        sort(a, a + cnt + 2);
        for (int i = 0; i < n; i++)
        {
            if (i != 0)
                cout << " ";
            cout << a[i];
        }
    }
    else
    {
        cout << "Heap Sort" << endl;
        int num = 0;
        int r = 0;
        while (num != n)
        {
            make_heap(a, a + n - r);
            pop_heap(a, a + n - r);
            num = 0;
            for (int i = 0; i < n; i++)
            {
                if (a[i] == b[i])
                    num++;
            }
            if (num == n)
            {
                r++;
                make_heap(a, a + n - r);
                pop_heap(a, a + n - r);
                for (int i = 0; i < n; i++)
                {
                    if (i != 0)
                        cout << " ";
                    cout << a[i];
                }
            }
            r++;
        }
    }
    system("pause");
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值