【堆排序 + 插入排序】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 
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);
}

 

先看后好习惯哦~

你可能感兴趣的:(题解,PAT甲级)