【堆排序 + 插入排序】PAT A1098 Insertion or Heap Sort (25分) (題目 + 代碼 + 詳細註釋 + 測試點4分析)

題目如下:

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

題目大意:給定兩組數 ,問第二組數是第一組數插入排序的中間過程還是堆排序的中間過程。判斷後,再按照這種排序執行一步,然後輸出結果

思路分析:

  • 法一:對第一個數組分別進行兩種排序,每進行一次,就和第二個數組比較,如果完全吻合,那麼就是這種排序
  • 法二:根據插入排序的特點。遍歷第二個數組,如果前一部分是升序的,並且後面的部分完全吻合,那麼就是插排

顯然,法二更優

下面是我AC的代碼及註釋

#include<bits/stdc++.h> 
using namespace std;
const int N = 105;

int n, a[N], temp[N];

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

void insertsort(int i) {          //插排,將數組temp中的第i個元素插到它應該在的位置
    printf("Insertion Sort\n");
    if (i == n + 1) return;             //如果temp已經是a排好序的(這句可有可無,本題並沒有這樣的測試點)
    int t = temp[i], j;              //插排典型代碼,不解釋
    for (j = i - 1; j >= 1 && temp[j] > t; j--)
        temp[j + 1] = temp[j];
    temp[j + 1] = t;
}

void heapsort() {                  //堆排序
    printf("Heap Sort\n");
    int i = n;
    while (i && temp[i] > temp[1])
        i--;                     //從後往前找到第一個小於temp[1]的元素,那個元素即爲根節點應該去的位置
    swap(temp[i--], temp[1]);
    downadj(1, i);           //向下調整法的堆排序
}

void read() {
    scanf("%d", &n);
    for (int i = 1; i <= n; i++)
        scanf("%d", &a[i]);
    for (int i = 1; i <= n; i++)
        scanf("%d", &temp[i]);
}

int main() { 
    read();          //讀入數據
    int i = 1, j;
    while (i < n && temp[i] <= temp[i + 1])  i++;      //從頭開始遍歷temp數組,找到不是升序的分界點。  注意這裏的temp[i] <= temp[i + 1]的等號(測試點4的坑)
    for (j = i + 1; j <= n && a[j] == temp[j]; j++)      //從分界點開始遍歷temp,判斷temp和a是否完全吻合
        ;
    if (j == n + 1)  insertsort(i + 1);         //j == n + 1說明完全吻合,執行一次插排
    else   heapsort();                         //否則執行一次堆排
   
    for (int i = 1; i <= n; i++)          //輸出處理過一次的temp數組
        printf("%d%s", temp[i], i < n ? " " : "\n");
    return 0;
}

另補充一點:其實insert函數可以用sort來實現,以節省碼量。附上代碼:

void insertsort(int i) {
    printf("Insertion Sort\n");
    sort(temp + 1, temp + i + 1);
}

 

先看後好習慣哦~

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