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 NNN (≤100\le 100≤100). Then in the next line, NNN integers are given as the initial sequence. The last line contains the partially sorted sequence of the NNN 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 resuling 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
贵校PTA作业,请自行绕道,以免不必要的麻烦#include<stdio.h> #include<stdlib.h> void PercolateDown(int sorted[],int N,int p) { int i = p; int key = sorted[i]; while(2 * i + 1<= N) { if(2 * i + 2 <= N && sorted[2 * i + 2] > sorted[2 * i + 1]) { if(key < sorted[2 * i + 2]) { sorted[i] = sorted[2 * i + 2]; i = 2 * i + 2; } else break; } else { if(key < sorted[2 * i + 1]) { sorted[i] = sorted[2 * i + 1]; i = 2 * i + 1; } else break; } } sorted[i] = key; } int main(void) { int N, temp, size, is_insert=0, whether; scanf("%d",&N); int initial[N]; int sorted[N]; for(int i=0; i<N; i++) { scanf("%d",&initial[i]); } for(int i=0; i<N; i++) { scanf("%d",&sorted[i]); } for(int i=0; i<N; i++) { for(whether=0; whether<N; whether++) { if(initial[whether]!=sorted[whether]) break; } if(whether==N) { is_insert=1; break; } else { temp=initial[i]; for(int j=0; j<i; j++) { if(temp<initial[j]) { for(int k=i; k>j; k--) { initial[k]=initial[k-1]; } initial[j]=temp; break; } } } } if(is_insert!=1) { printf("Heap Sort\n"); for(int i=N-1; i>=0; i--) { if(sorted[i]<sorted[0]) { size=i; break; } } temp=sorted[0]; sorted[0]=sorted[size]; sorted[size]=temp; PercolateDown(sorted,size - 1,0); for(int i=0; i<N-1; i++) { printf("%d ",sorted[i]); } printf("%d\n",sorted[N-1]); } else { printf("Insertion Sort\n"); for(int i=1; i<N; i++) { if(sorted[i] < sorted[i-1]) { for(int j=0; j<i; j++) { temp=sorted[i]; if(temp<sorted[0]) { for(int k=i; k>0; k--) { sorted[k]=sorted[k-1]; } sorted[0]=temp; break; } if(sorted[j]<temp && sorted[j+1]>temp) { for(int k=i; k>=j+2; k--) { sorted[k]=sorted[k-1]; } sorted[j+1]=temp; break; } } break; } } for(int i=0; i<N-1; i++) { printf("%d ",sorted[i]); } printf("%d\n",sorted[N-1]); } }
排序算法辨识
本文介绍了一种通过部分排序结果来判断使用的是插入排序还是堆排序的方法,并提供了具体的输入输出示例及实现代码。
585

被折叠的 条评论
为什么被折叠?



