According to Wikipedia:
Insertion sort iterates, consuming one input element each repetition, and growing a sorted output list. At 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 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
题目大意:先介绍了一下插入排序和堆排序,然后给出两个序列,第一个序列是无序的初始序列,第二个序列指用堆排序或插入排序的某一步迭代的序列,我们的目标是找出用的是哪中迭代方法,并写出下一个迭代序列
题目思路:很简单,就是写一个插排和堆排,然后在每一次迭代之后看和题目序列是否一样(可以用vector的==轻松实现),之后用一个flag变量来标记即可
这个题没什么坑(注意最后一个数字后不能有空格),只要你了解两个排序即可
请大家注意堆排序建堆的时候一定要有堆的大小那个参数,最后堆排序的时候是按照迭代次数逐渐减小堆的大小的
#include
#include
using namespace std;
bool insertion_sort(vector &a,vector &b)
{
bool flag=false;
for(int i=1;i=0)
{
a[j+1]=a[j];
j--;
}
a[j+1]=temp;
if(flag)
{
return true;
}
if(a==b)
{
flag=true;
}
}
return false;
}
void swap(vector &arry,int a,int b)
{
int t=arry[a];
arry[a]=arry[b];
arry[b]=t;
}
void adjust_heap(vector &a,int j,int m)
{
int left=j*2+1;
int right=left+1;
int max;
if(lefta[j])
max=left;
else
max=j;
if(righta[max])
max=right;
if(max!=j)
{
swap(a,max,j);
adjust_heap(a,max,m);
}
}
void build_heap(vector &a)
{
int begin=(int)a.size()/2-1;//注意是减一
for(int i=begin;i>=0;i--)
{
adjust_heap(a,i,a.size());
}
}
bool heap_sort(vector &a,vector &b)
{
build_heap(a);//构建大顶堆
bool flag=false;
for(int i=a.size()-1;i>=1;i--)
{
swap(a,0,i);
adjust_heap(a,0,i);//一定注意这里是堆的大小是变小的,第三项一定要填i
if(flag)
{
return true;
}
if(a==b)
{
flag=true;
}
}
return false;
}
int main()
{
int n;
int t;
vector initial;
vector initial2;
vector sorting;
scanf("%d",&n);
for(int i=0;i