1098 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?

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

 考点是插入排序和堆排序

这道题有点难理解诶,如果对堆排序插入排序不熟看答案也不好看出来

题意:给出长度为 n 的两个序列 a,b,a为原始序列,b为其排序的一个步骤结果,让你看这排序是插入排序还是堆排序。并且输出它的下一次排序序列

分析:

1. 插入排序的特点是:b数组前面的顺序是有序的(min->max),后面的顺序与原数组a是相同的,所以判断方法是遍历前面几位。遇到顺序不对的情况,就判断此处数组b和数组的对应位置的值是不是相等,如果是就说明是插入排序,否则是堆排序。

2. 插入排序的下一步就是把第一个不符合从小到大的顺序的那个元素插入到前面已经排好序的序列的合适位置,所以只要对前几个已经排好序的后面一位的这个序列sort排序即可

3.堆排序的特点是:b数组后面是有序的(min->max),前面的顺序不一定,那么就可以从后面扫描,扫描到第一个数比前面那个数小的数字,记录其位置ind,和第一个数字交换,然后把 1~ind-1进行一次向下调整,i和n是需要调整的区间,因为是大顶堆,所以就是不断比较当前结点和自己的孩子结点那个大,如果孩子结点大就交换,然后再不断调整直到到达区间的最大值直到不能再继续为止

#include <bits/stdc++.h>
using namespace std;
int n;
int a[101], b[101];
void downJust(int i, int n) {
int j = 2 * i;
while(j <= n) {
if(j + 1 <= n && b[j] < b[j + 1]) ++j;
if(b[j] > b[i]) {
swap(b[i], b[j]);
i = j;
j *= 2;
} else break;
}

}
int main() {
memset(a, 0, sizeof(a)); memset(b, 0, sizeof(b));
cin >> n;
for(int i = 1; i <= n; i++) cin >> a[i];
for(int i = 1; i <= n; i++) cin >> b[i];
int ind = 2, pos;
while(ind <= n && b[ind - 1] <= b[ind]) ind++; //前面 min->max
pos = ind;
while(ind <= n && a[ind] == b[ind]) ind++; //后面 a[] = b[]
//说明是插入排序
if(ind == n + 1) {
cout << "Insertion Sort" << endl;
sort(b + 1, b + pos + 1);
} else {
cout << "Heap Sort" << endl;
ind = n;
while(ind > 1 && b[ind - 1] <= b[ind]) ind--;
swap(b[1], b[ind]);
downJust(1, ind - 1);
}
for(int i = 1; i <= n; i++) {
cout << b[i];
if(i != n) {
cout << " ";
} else cout << endl;
}

return 0;
}