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
题意为对于一个给定的序列,在一定的排序(插入或堆排序)后,对于得到的部分排好序的序列,确定使用的是哪一种方式,并且给出继续使用该种方法进行下一轮排序后的序列。
基本方法是对于每一轮的排序完成后对给出的序列进行匹配,成功就完成。
#include <stdio.h>
#include <stdlib.h>
int Insert_decide(int a[],int N,int get[]);
void Adjustdown(int a[], int i, int N);
int Heap_decide(int a[], int N, int get[]);
int main (){
int N;
scanf("%d", &N);
int *a = (int *)malloc(sizeof(int) * N);
int *get = (int *)malloc(sizeof(int) * N);
int *aheap = (int *)malloc(sizeof(int) * (N+1));
int *getheap = (int *)malloc(sizeof(int) *(N+1));
for(int i = 0; i<N; i++){
scanf("%d",a+i);
aheap[i+1] = a[i];
}for(int i = 0; i<N; i++){
scanf("%d",get+i);
getheap[i+1] = get[i];
}
int i = Insert_decide(a,N,get);
if(i){
printf("Insertion Sort\n");
i++;
int temp = a[i];
int j;
for(j = i - 1;j >= 0 ;j--){
if(temp < a[j]){
a[j+1] = a[j];
}else break;
}a[j+1] = temp;
printf("%d",a[0]);
for(int j = 1; j<N ;j++){
printf(" %d",a[j]);
}
}
i = Heap_decide(aheap,N,getheap);
if(i){
i--;
printf("Heap Sort\n");
int temp = aheap[1];
aheap[1] = aheap[i];
aheap[i] = temp;
Adjustdown(aheap,1,i-1);
printf("%d",aheap[1]);
for(int j = 2; j<=N ;j++){
printf(" %d",aheap[j]);
}
}
return 0;
}
int Insert_decide(int a[],int N,int get[]){
int i ,j;
for(i = 1;i < N; i++){
int state = 1;
int temp = a[i];
for(j = i - 1;j >= 0 ;j--){
if(temp < a[j]){
a[j+1] = a[j];
}else break;
}a[j+1] = temp;
for(j = 0;j < N ;j++){
if(a[j] != get[j])
state = 0;
}
if(state){
return i;
}
}
return 0;
}
void Adjustdown(int a[], int i, int N){
if(2*i > N) return;
Adjustdown(a,2*i,N);
Adjustdown(a,2*i+1,N);
if(2*i == N){
int x = a[2*i];
if(x > a[i]){
a[2*i] = a[i];
a[i] = x;
}
}else {
int x = a[2*i];
int y = a[2*i+1];
if(x > y){
if(x > a[i]){
a[2*i] = a[i];
a[i] = x;
}Adjustdown(a,2*i,N);
}else {
if(y > a[i]){
a[2*i+1] = a[i];
a[i] = y;
}Adjustdown(a,2*i+1,N);
}
}
}
//1~N are the numbers
int Heap_decide(int a[], int N, int get[]){
int i;
for( i = N/2; i >0; i--){
Adjustdown(a,i,N);
}
for(i = N;i>0;i--){
int state =1;
int temp = a[1];
a[1] = a[i];
a[i] = temp;
Adjustdown(a,1,i-1);
for(int j = 1;j <=N;j++){
if(a[j] != get[j])
state = 0;
}if(state){
return i;
}
}
return 0;
}