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.
输入规格:每个输入文件包含一个测试样例,正对每个样例,第一行包含一个正整数N,然后接下来一行N个正整数给你初始序列,最后一行包含,排序序列的结果。假设目标排序总是升序的。所有的数字总是按照空格隔开的。
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.
针对每个测试案例,打印一行要么插入排序,要么堆排序,表示用于获得部分结果的方法。然后在使用一次这样的迭代,第二行输出结果序列,保证答案是针对每个测试文件是唯一的。在一行所有的数字有一个1空格,最后一行没有空格
核心思路
请默写:插入排序sort版本与堆排序(25分)
完整代码
#include<cstdio>
#include<algorithm>
using namespace std;
const int N = 111;
int origin[N], tempOri[N], changed[N];//原始数组,原始数组备份及目标数组
int n; // 元素个数
bool isSame(int A[], int B[]){
for(int i =1;i<=n;i++){
if(A[i] != B[i]) return false;
}
return true;
}
bool showArray(int A[]){
for(int i =1;i<=n;i++){
printf("%d",A[i]);
if(i<n) printf(" ");
}
printf("\n");
}
bool insertSort(){
bool flag = false;
for(int i =2;i<=n;i++){
if(i!=2 && isSame(tempOri,changed)){
flag = true; //中间步骤与目标相同,且不是初始序列
}
//插入部分直接用sort代替
sort(tempOri,tempOri+i+1);
if(flag == true){
return true;//如果flag为true,则说明已达到目标数组,返回true
}
}
return false;//无法达到目标数组返回false
}
//对heap数组在[low,high]范围进行调整
//其中low为欲调整结点的数组下标high一般为堆的最后一个元素的数组下标
void downAdjust(int low,int high){
int i = low,j = i*2; //i就是欲调整结点,j为其左孩子结点
while(j<=high){//存在孩子结点
//如果右孩子结点存在,且右孩子结点的值大于左孩子结点
if(j+1 <= high && tempOri[j+1]>tempOri[j]){
j = j+1;
}
//如果孩子结点中最大的权值比父亲结点大
if(tempOri[j] > tempOri[i]) {
swap(tempOri[j],tempOri[i]);//交换最大权值的孩子结点与父亲结点
i = j;
j = i*2;
}else{
break;//孩子结点的权值均比父亲结点的小,调整结束.
}
}
}
void heapSort(){
bool flag = false;
for(int i =n/2;i>=1;i--){
downAdjust(i,n);//建堆
}
for(int i =n;i>1;i--){
if(i != n && isSame(tempOri,changed)){
flag = true;//中间步骤与目标相同,且不是初始序列
}
swap(tempOri[i],tempOri[1]);
downAdjust(1,i-1);//调整堆顶
if(flag == true){
showArray(tempOri); // 已达到目标数组,返回true
return ;
}
}
}
int main(){
scanf("%d",&n);
for(int i =1;i<=n;i++){
scanf("%d",&origin[i]);
tempOri[i] = origin[i];
}
for(int i = 1;i<=n;i++){
scanf("%d",&changed[i]);
}
if(insertSort()){
printf("Insertion Sort\n");
showArray(tempOri);
}else{ //到达此处时一定是堆排序
printf("Heap Sort\n");
for(int i =1;i<=n;i++){
tempOri[i] = origin[i]; //还原tempOri数组
}
heapSort(); //堆排序
}
return 0;
}