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
按照给出序列和某趟结果,判断是插入排序还是堆排序(大)
根据插入排序特性来判断性质。如果一段序列有序,且上下不匹配的position之后的匹配结果是上下文一致,则是插入排序。否则定为堆排序。
如果是插入排序,将结果序列和position作为参数,进行下一趟排序。相关算法可以参考本科教材,反复熟练。将结果输出。
如果是堆排序,则对初始序列进行排序,与结果序列比较,得出在末尾处已经有序的position。然后将堆中的第一个元素(也就是该趟中最大值)调到堆尾,堆size减一。为了方便计算,重新赋值数组1->N。对堆进行下一步调整。相关算法参考教材。结果输出。
#include<iostream>
#include<algorithm>
using namespace std;
int init_sequence[100];
int sorted_sequence[100];
bool isInsertSort = true;
void InsertNextOne(int sorted_sequence[],int unsorted_pos) {
int temp;
int i;
if(unsorted_pos == 0) {
return;
}
if(sorted_sequence[unsorted_pos] < sorted_sequence[unsorted_pos - 1]) {
temp = sorted_sequence[unsorted_pos];
sorted_sequence[unsorted_pos] = sorted_sequence[unsorted_pos - 1];
for(i = unsorted_pos - 2; i >= 0 && sorted_sequence[i] > temp; i--) {
sorted_sequence[i + 1] = sorted_sequence[i];
}
if(i < 0) {
sorted_sequence[0] = temp;
} else {
sorted_sequence[i + 1] = temp;
}
}
}
void HeapAjust(int sorted_sequence[],int start,int end){
int rc = sorted_sequence[start];
int j;
for(j = 2 * start;j <= end;j *= 2 ){
if(j < end && sorted_sequence[j] < sorted_sequence[j + 1]){
j++;
}
if(rc >= sorted_sequence[j]){
break;
}
sorted_sequence[start] = sorted_sequence[j];
start = j;
}
sorted_sequence[start] = rc;
}
int main() {
int N;
cin>>N;
for(int i = 0; i < N; i++) {
cin>>init_sequence[i];
}
int sorted_pos,unsorted_pos;
bool flag = true;
bool detected = true;
for(int i = 0; i < N; i++) {
cin>>sorted_sequence[i];
//输入的同时进行上下文判断
if(i != 0 && flag && sorted_sequence[i] < sorted_sequence[i - 1]) {
flag = false;
sorted_pos = i - 1;
unsorted_pos = i;
}
if(!flag) {
if(detected && init_sequence[i] != sorted_sequence[i]) {
isInsertSort = false;
detected = false;
}
}
}
if(isInsertSort) {
cout<<"Insertion Sort"<<endl;
InsertNextOne(sorted_sequence,unsorted_pos);
for(int i = 0; i < N; i++) {
cout<<sorted_sequence[i];
if(i < N - 1 ) {
cout<<" ";
}
}
} else {
cout<<"Heap Sort"<<endl;
//定位堆排序范围 0 -> unsorted_pos
sort(init_sequence,init_sequence + N);
for(int i = N - 1; i >= 0; i--) {
if(sorted_sequence[i] != init_sequence[i]) {
unsorted_pos = i;
break;
}
}
int temp = sorted_sequence[unsorted_pos];
sorted_sequence[unsorted_pos] = sorted_sequence[0];
sorted_sequence[0] = temp;
unsorted_pos--;
int length = (unsorted_pos + 1) / 2;
int s[101];
for(int i = 1;i <= N;i++){
s[i] = sorted_sequence[i - 1];
}
HeapAjust(s,1,unsorted_pos + 1);
for(int i = 1; i <= N; i++) {
cout<<s[i];
if(i < N ) {
cout<<" ";
}
}
}
return 0;
}
前辈传送门...