PAT甲级1098. Insertion or Heap Sort (25)

题目

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.

Heap sort divides its input into a sorted and an unsorted region, and it iteratively shrinks the unsorted region by extracting the largest element and moving that to the sorted region. it involves the use of a heap data structure rather than a linear-time search to find the maximum.

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?

输入格式

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.

输出格式

For each test case, print in the first line either “Insertion Sort” or “Heap 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.

输入样例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


PAT链接

思路

对原序列进行一步一步插入排序(就是挨个sort)

sort(insertion.begin(), insertion.begin() + i);

然后如果其中某一步产生的序列与结果序列相同,说明是插入排序,然后i+1,接着sort
如果不是插入排序,那肯定是堆排序。只需从后找出第一个不满足顺序的下标p,然后交换heap[1]和heap[p],接下来做一步percolateDown即可

代码

/**
* @tag     PAT_A_1098
* @authors R11happy (xushuai100@126.com)
* @date    2017-3-10 17:34-19:30
* @version 2.0
* @Language C++
* @Ranking  2240/409
* @function null
*/

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;

int n;
int insertFlag = 0;
vector<int> origin, res, insertion, heap;


void insertSort()
{
    for (int i = 2; i<n; i++)
    {
        if (insertion == res)
        {
            insertFlag = 1;
            sort(insertion.begin(), insertion.begin() + i);
            if(i>2) break;  //初始序列不参与比较
        }
        else
        {
            sort(insertion.begin(), insertion.begin() + i);
        }
    }
}

void percolateDown(int low, int high)
{
    int i = low, j = i * 2;
    while (j <= high)
    {
        if (j + 1 <= high && heap[j] <heap[j + 1]) j = j + 1;
        if (heap[i] < heap[j])
        {
            swap(heap[i], heap[j]);
            i = j;
            j = i * 2;
        }
        else
        {
            break;
        }
    }
}

int main(int argc, char const *argv[])
{
    int input;
    scanf("%d", &n);
    for (int i = 0; i<n; i++)
    {
        scanf("%d", &input);
        origin.push_back(input);
    }
    heap.push_back(-1);
    for (int i = 0; i<n; i++)
    {
        scanf("%d", &input);
        res.push_back(input);
        heap.push_back(input);
    }
    insertion = origin;
    insertSort();
    if (insertFlag == 1)
    {
        printf("Insertion Sort\n");
        printf("%d", insertion[0]);
        for (int i = 1; i<n; i++)
        {
            printf(" %d", insertion[i]);
        }
        printf("\n");
    }
    else
    {
        printf("Heap Sort\n");
        int p = n;
        while(p>=2 && heap[p] > heap[p-1])  p--;
        swap(heap[1], heap[p]);
        percolateDown(1, p-1);
        printf("%d", heap[1]);
        for (int i = 2; i <= n; i++)
        {
            printf(" %d", heap[i]);
        }
        printf("\n");

    }
    return 0;
}

收获
1. sort来实现插入排序
2. 注意origin序列与res序列相同的情况
3. 当确定是堆排序的时候,只需找到是堆排序进行到了哪一步,然后接着percolateDown即可,不用重新建堆

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值