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
#include
#define MAX 100
int BIAO=0;/*标记的,只能有个一个排序是成功的*/
int BIJIAO(int n,int A[],int B[])/*比较函数*/
{
for(int i=0;i<n;i++)
if(A[i]!=B[i])
return 0;
return 1;
}
void Sort(int n,int A[],int B[])/*插入排序*/
{
int i,j;
int k;
int tep;
int sum=0;
int flag=0;/*这里我设置的变量太多了,应该有更好的方法,自己想想*/
sum=BIJIAO(n,A,B);
for(i=1;i<n;i++)
{
tep=A[i];
for(j=i;j>0&&A[j-1]>tep;j--)
A[j]=A[j-1];
A[j]=tep;
if(sum==0)
sum=BIJIAO(n, A, B);
if(sum==1)
{
if(flag)/*成果对比后,在迭代一次*/
break;
flag=1;
}
}
if(sum!=1&&i==n)
sum=BIJIAO(n,A,B);
if(sum==1)/*输出,并且标记为1,代表输出插入排序了*/
{
BIAO=1;
printf("Insertion Sort\n");
for(i=0;i<n;i++)
{
if(i!=0)
printf(" ");
printf("%d",A[i]);
}
}
}
void HUAN(int *a,int *b)/*堆排序的交换*/
{
int t=*a;
*a=*b;
*b=t;
}
void Perdomn(int A[],int p,int N)/*最大堆的调整函数*/
{
int par,ch;
int x;
x=A[p];
for(par=p;(2*par+1)<N;par=ch)
{
ch=par*2+1;
if((ch!=N-1)&&(A[ch]<A[ch+1]))
ch++;
if(x>=A[ch])
break;
else
A[par]=A[ch];
}
A[par]=x;
}
void Duipaixu(int n,int A[],int B[])
{
int i,sum=0;
for(i=n/2-1;i>=0;i--)/*建立最大堆*/
Perdomn(A,i,n);
for(i= n-1;i>0;i--)
{
if(sum!=1)
{
sum=BIJIAO(n,A,B);
HUAN(&A[0],&A[i]);/*交换位置*/
Perdomn(A,0,i);/*开始逐个迭代,建立出有序序列*/
}
else
break;
}
if(sum==1)/*输出*/
{
printf("Heap Sort\n");
for(i=0;i<n;i++)
{
if(i!=0)
printf(" ");
printf("%d",A[i]);
}
}
}
int main()
{
int i,j;
int A[MAX],B[MAX],C[MAX],D[MAX];
scanf("%d",&j);
for(i=0;i<j;i++)
{
scanf("%d",&A[i]);
C[i]=A[i];
}
for(i=0;i<j;i++)
{
scanf("%d",&B[i]);
D[i]=B[i];
}
Sort(j,A,B);
if(BIAO==0)/*标记*/
Duipaixu(j,C,D);
return 0;
}
/*6
1 3 2 6 5 4
1 2 3 5 6 4*/