题目
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
思路
类似于PAT甲级真题 1089 Insert or Merge (25分)
1. 判定是否为插入排序
记两个数组分别为a、b。
首先定位数组b中当前未排序元素位置k,若a、b后面的元素相同:
if (equal(a.begin()+k, a.end(), b.begin()+k))
则可判定为Insertion Sort。
用到了algorithm.h
中的equal
函数,定义是:
bool equal (InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2);
2. 对于插入排序
直接调用sort函数,将下一位纳入已排序范围即可。
3. 对于堆排序
首先找出当前未排序位置边界:
k = n - 1;
while (k>0 && b[k]>=b[0]) k--; //未排序位置边界
当前堆顶是未排序元素的最大值,已排序元素都是比该值还要大的。
然后再进行一次堆排序,步骤是:
- 将堆顶与最后一个元素交换,接入有序区;
- 重新建堆。从根节点向下,将父节点与子节点中较大者交换,交换后递归处理受影响的子树。重点要掌握sift()建堆函数。
代码
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void sift(vector<int> &b, int i, int n){
int l = 2 * i + 1;
int r = 2 * i + 2;
if (l >= n) return; //到达叶子
int maxI = i; //记录i、l、r中最大值位置
if (l<n && b[l]>b[maxI]) maxI = l;
if (r<n && b[r]>b[maxI]) maxI = r;
if (maxI != i) {
swap(b[i], b[maxI]);
sift(b, maxI, n);
}
}
int main(){
int n;
cin >> n;
vector<int> a(n), b(n);
for (int i=0; i<n; i++){
cin >> a[i];
}
for (int i=0; i<n; i++){
cin >> b[i];
}
int k = 1; //首个逆序位置
while (k<n && b[k-1]<=b[k]) k++;
//b剩下的逆序部分是否与a后面部分相同,若相同就是Insertion Sort
if (equal(a.begin()+k, a.end(), b.begin()+k)){
cout << "Insertion Sort" << endl;
sort(b.begin(), b.begin()+k+1);
}
else{
cout << "Heap Sort" << endl;
k = n - 1;
while (k>0 && b[k]>=b[0]) k--; //未排序位置边界
swap(b[0], b[k]); //将当前最大值放到排序区
sift(b, 0, k);
}
cout << b[0];
for (int i=1; i<n; i++){
cout << " " << b[i];
}
return 0;
}