5-1 Insertion or Heap Sort   (9分)

来源:互联网 发布:社会主义中级阶段知乎 编辑:程序博客网 时间:2024/05/16 10:51
5-1 Insertion or Heap Sort   (9分)

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 integerNNN (≤100\le 100100). Then in the next line, NNN integers are given as the initial sequence. The last line contains the partially sorted sequence of theNNN 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:

103 1 2 8 7 5 9 4 6 01 2 3 7 8 5 9 4 6 0

Sample Output 1:

Insertion Sort1 2 3 5 7 8 9 4 6 0

Sample Input 2:

103 1 2 8 7 5 9 4 6 06 4 5 1 0 3 2 7 8 9

Sample Output 2:

Heap Sort5 4 3 1 0 2 6 7 8 9
#include<iostream>#include<vector>using namespace std;void insert_sort(int *a,int *b,int N){int i=N-1,j,k;while(b[i]==a[i]){i--;if(i==-1){int fir=0;i=0;while(b[i+1]>b[i])i++;i++;for(j=0;j<i;j++)if(b[j]>b[i])break;for(k=0;k<j;k++)cout<<b[k]<<" ";cout<<b[i];for(k=j;k<i;k++)cout<<" "<<b[k];for(j=i+1;j<N;j++)cout<<" "<<b[j];return;}}i++;/*if(i==N){int fir=0;for(i=0;i<N;i++){if(fir==0)fir=1;elsecout<<" ";cout<<b[i];}return ;}*/for(j=0;j<i;j++)if(b[j]>b[i])break;for(k=0;k<j;k++)cout<<b[k]<<" ";cout<<b[i];for(k=j;k<i;k++)cout<<" "<<b[k];for(j=i+1;j<N;j++)cout<<" "<<b[j];}void heap_sort(int *a,int*b,int N){int i;int j,k,temp;int *ismax=new int[N];int max=100000000;for(i=N-1;i>=0;i--){ismax[i]=0;if(b[i]<max){max=b[i];ismax[i]=1;//cout<<i<<"&";}}//cout<<endl;max=-1;for(i=0;i<N;i++){if(max<b[i]){max=b[i];//cout<<i<<"*";if(ismax[i]==1)break;}}//cout<<endl;k=b[0];b[0]=b[i-1];for(j=i-1;j>=1;j--)b[j]=b[j-1];j=1;while(((b[j]<b[2*j+1])||(b[j]<b[2*j]))&&2*j<i){if(b[2*j+1]>b[2*j]&&(2*j+1<i)){temp=b[j];b[j]=b[2*j+1];b[2*j+1]=temp;j=2*j+1;}else {temp=b[j];b[j]=b[2*j];b[2*j]=temp;j=2*j;}}for(j=1;j<=i-1;j++)cout<<b[j]<<" ";cout<<k;for(j=i;j<N;j++)cout<<" "<<b[j];}int main(){int N,i,j;cin>>N;int *number=new int[N];int *ques=new int[N];for(i=0;i<N;i++)scanf("%d",&number[i]);for(i=0;i<N;i++)scanf("%d",&ques[i]);if(ques[1]>ques[0]){cout<<"Insertion Sort"<<endl;insert_sort(number,ques,N);}else{cout<<"Heap Sort"<<endl;heap_sort(number,ques,N);}}
感想:
1.这道题并没有必要真的要去做堆排序和插入排序,只需要判断是哪种情况然后模拟这个过程就可以了
2.注意进行一步插入排序不能没有任何变化,也就是说进行一步如果没有变化那就再进行一步这样循环直到有变化
3.有个点是没有进行任何变化,注意这样可以判断出一定是插入排序,因为堆排序开始的时候就已经是堆状态
0 0