09-排序3 Insertion or Heap Sort

142 阅读3分钟

Problem Description

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?

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 "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 resulting 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 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

Solution

题意理解:给出待排序数组和排序过程中迭代到某一步之后的顺序,要求这个顺序是由插入排序生成的还是堆排序生成的,并且输出再迭代一次后的顺序。

注意判断第一种排序算法时要先赋值原数组到临时数组,否则判断第二种算法时没有原数组

插入排序:

从下标为 1 的元素开始向前遍历,找到插入位置后插入,保证 p 之前的数都是排好序的。(稳定)

堆排序:

先将整个数组初始化为一个最大堆,然后每次把最大堆的顶元素与尾元素交换,再把去除尾元素的堆更新为最大堆,以此类推。从后到前生成非递减序列的排序。(不稳定)

#include <stdio.h>
#include <stdlib.h>

int IsSame(int A[], int N, int target[])
{
    int i, flag = 1;;
    for (i = 0; i < N && flag; i++)
        if (A[i] != target[i]) flag = 0;
    return flag;
}

int InserstionSort(int A[], int N, int target[])
{
    int i, p, tmp, flag = 0;
    int a[100];
    for (i = 0; i < N; i++) a[i] = A[i];

    for (p = 1; p < N && flag == 0; p++) {
        tmp = a[p];
        for (i = p; i > 0 && a[i - 1] > tmp; i--)
            a[i] = a[i - 1];
        a[i] = tmp;
        if (IsSame(a, N, target)) flag = 1;
    }

    if (flag) {
        tmp = a[p];
        for (i = p; i > 0 && a[i - 1] > tmp; i--)
            a[i] = a[i - 1];
        a[i] = tmp;
        printf("Insertion Sort\n");
        for (i = 0; i < N; i++)
            if (i) printf(" %d", a[i]);
            else printf("%d", a[i]);
    }
    return flag;
}

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

void HeapSort(int A[], int N, int target[])
{
    int i, tmp, flag = 0;

    for (i = N / 2 - 1; i >= 0; i--)
        PercDown(A, i, N);
    
    for (i = N - 1; i > 0 && flag == 0; i--) {
        tmp = A[0];
        A[0] = A[i];
        A[i] = tmp;
        PercDown(A, 0, i);
        if (IsSame(A, N, target)) flag = 1;
    }

    tmp = A[0];
    A[0] = A[i];
    A[i] = tmp;
    PercDown(A, 0, i);
    printf("Heap Sort\n");
    for (i = 0; i < N; i++)
        if (i) printf(" %d", A[i]);
        else printf("%d", A[i]);
}

int main()
{
    int i, N, A[100], target[100];
    scanf("%d", &N);
    for (i = 0; i < N; i++) scanf("%d", &A[i]);
    for (i = 0; i < N; i++) scanf("%d", &target[i]);
    if (InserstionSort(A, N, target)) ;
    else HeapSort(A, N, target);
    return 0;
}