1098 Insertion or Heap Sort (25point(s)) - C語言 PAT 甲級

1098 Insertion or Heap Sort (25point(s))

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:

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

Sample Output:

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

Sample Input:

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

Sample Output:

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

題目大意:

輸入 N 個數的序列,第一行是原始序列,第二行是插入排序或堆排序的某時刻

輸出判斷是什麼排序,並輸出第二個序列的下一步

設計思路:
  1. 插入排序的判斷:
    (開頭一部分一定有序)&&(無序部分與原序列一致)

  2. 堆排序的判斷:
    排除法

  3. 插入排序迭代一次:
    從斷點迭代一次即可

  4. 堆排序迭代一次,題目是大頂堆:

    1. 從後往前找第一個比堆頂元素小的元素,迭代一次堆排即可
編譯器:C (gcc)
#include <stdio.h>
#include <stdlib.h>

int cmp(const void *a, const void *b)
{
        return *((int*)a) - *((int*)b);
}

int swap(int *a, int *b)
{
        int temp = *b;
        *b = *a;
        *a = temp;
        return 0;
}

int nextheap(int arr[], int start, int end)
{
        int dad = start;
        int son = dad * 2 + 1;
        while (son <= end) {
                if (son + 1 <= end && arr[son] < arr[son + 1]) {
                        son++;
                }
                if (arr[dad] > arr[son]) {
                        return 0;
                } else {
                        swap(&arr[dad], &arr[son]);
                        dad = son;
                        son = dad * 2 + 1;
                }
        }
        return 0;
}

int main(void)
{
        int n, num[100], half[100];
        int lenth, i;

        scanf("%d", &n);
        for (i = 0; i < n; i++) {
                scanf("%d", &num[i]);
        }
        for (i = 0; i < n; i++) {
                scanf("%d",&half[i]);
        }
        for (i = 0; i < n - 1 && half[i] <= half[i + 1]; i++) {
                ;
        }
        for (i++, lenth = i; i < n && half[i] == num[i]; i++) {
                ;
        }
        if (i >= n) {
                printf("Insertion Sort\n");
                qsort(half, lenth + 1, sizeof(num[0]), cmp);
        } else {
                printf("Heap Sort\n");
                for (i = n -1; i > 1 && half[i] >= half[0]; i--) {
                        ;
                }
                swap(&half[i], &half[0]);
                nextheap(half, 0, i - 1);
        }

        printf("%d", half[0]);
        for (i = 1; i < n; i++) {
                printf(" %d", half[i]);
        }

        return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章