注意堆排序的调整 找到一个大的到堆顶
#include<bits/stdc++.h>
using namespace std;
//堆排序是每次找到一个最大的放到最后
const int MAXN = 110;
int a[MAXN];
int b[MAXN];
void downAdjust(int a[],int low,int height)
{
int i = 1;
int j = i * 2;
while(j <= height)
{
if(j + 1 <= height && a[j] < a[j + 1])
{
j = j + 1;
}
if(a[i] >= a[j])
{
break;
}
swap(a[i],a[j]);
i = j;
j = i * 2;
}
}
int main(void)
{
freopen("pat0314/in.txt","r",stdin);
int n;
cin>>n;
for(int i = 1;i<=n;i++)
{
cin>>a[i];
}
for(int i = 1;i<=n;i++)
{
cin>>b[i];
}
int i,j;
// for(int i = 1;i<=n;i++)
// {
// cout<<a[i];
// }
for(i = 1;i<=n - 1 && b[i] <= b[i + 1];i++);
for(j = i + 1;j <= n && a[j] == b[j];j++);
if(j == n + 1)
{
cout<<"Insertion Sort"<<endl;
sort(a + 1,a + i + 2);
for(int i = 1;i<=n;i++)
{
if(i == 1)
{
printf("%d",a[i]);
}
else
{
printf(" %d",a[i]);
}
}
}
else
{
cout<<"Heap Sort"<<endl;
j = n;
while(j > 2 && b[j] >= b[1])
{
j--;
}
swap(b[1],b[j]);
downAdjust(b,1,j - 1);
for(int i = 1;i<=n;i++)
{
if(i == 1)
{
printf("%d",b[i]);
}
else
{
printf(" %d",b[i]);
}
}
}
return 0;
}