PTA 09-排序3 Insertion or Heap Sort(25 分)

09-排序3 Insertion or Heap Sort(25 分)

题目地址:09-排序3 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.


解题方法:
对于插入排序的方式与前一题一样,而对于堆排序的求解我采用的方式是直接写一个堆排序,然后每次执行都判断一次是否和中间过程相同,这里建议在判断的时候从0开始判断,因为每次从头取出一个元素后,数组都会进行堆的结构构造,因此,从0开始判断可以减少很多比较的次数。
此外,如果您有更好的方法,欢迎留言,让我也好学习学习~


程序:

#include <iostream>
#include <algorithm>
#include <stdio.h>
using namespace std;

void PrecDown(int A[], int N, int p)
{
    int parent, child, temp = A[p];
    for (parent = p; parent*2+1 < N; parent = child)
    {
        child = parent * 2 + 1;
        if (child != N-1 && A[child+1] > A[child])
            child++;
        if (A[child] < temp)
            break;
        else
            A[parent] = A[child];
    }
    A[parent] = temp;
}

void heap_sort(int A[], int B[], int N)
{
    int flag;
    for (int i = N/2; i >= 0; i--)
        PrecDown(A, N, i);
    for (int i = N-1; i >= 0; i--)
    {
        flag = 0;
        swap(A[0], A[i]);
        PrecDown(A, i, 0);
        for (int j = 0; j < N; j++)
        {
            if (A[j] != B[j])
            {
                flag = 1;
                break;
            }
        }
        if (flag == 0)
        {
            printf("Heap Sort\n");
            swap(A[0], A[--i]);
            PrecDown(A, i, 0);
            printf("%d", A[0]);
            for (int k = 1; k < N; k++)
                printf(" %d", A[k]);
            return;
        }
    }
}

int main(int argc, char const *argv[])
{
    int N, i, flag = 0, j, k;
    cin >> N;
    int Arr[N], MedArr[N];
    for (i = 0; i < N; i++)
        cin >> Arr[i];  // 输入起始序列
    for (i = 0; i < N; i++)
        cin >> MedArr[i];  // 输入中间结果序列
    for (i = 1; i < N && MedArr[i-1] <= MedArr[i]; i++);    
    for (j = i; j < N; j++)
        if (Arr[j] != MedArr[j])
            flag = 1;   // 如果后面序列不一致说明肯定不是插入排序
    if (!flag)
    {   /* 如果是插入排序 */
        printf("Insertion Sort\n");
        sort(MedArr, MedArr+i+1);   // 在进行一次插入排序(这里用sort模拟插入排序)
        for (k = 0; k < N; k++)
        {
            if (k != 0)     /* 格式化输出 */
                printf(" ");
            printf("%d", MedArr[k]);
        }
    }
    else
    {   /* 如果是堆排序 */
        heap_sort(Arr, MedArr, N);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值