1098 Insertion or Heap Sort (25 分)(插入排序 堆排序
作者:互联网
#include<bits/stdc++.h>
using namespace std;
const int N=200;
int arr[N];
int brr[N];
void down(int u,int n){
int t=u;
if(u*2<=n&&brr[u*2]>brr[u])t=u*2;
if(u*2+1<=n&&brr[u*2+1]>brr[t])t=u*2+1;
if(t!=u){
swap(brr[u],brr[t]);
down(t,n);
}
}
int main(){
int n;
cin>>n;
for(int i=1;i<=n;i++){
cin>>arr[i];
}
for(int i=1;i<=n;i++){
cin>>brr[i];
}
int p;
for( p=2;p<=n;){
if(brr[p]>=brr[p-1])p++;
else break;
}
int c=p;
for(;p<=n;){
if(arr[p]==brr[p])p++;
else break;
}
if(p==n+1){
cout<<"Insertion Sort\n";
sort(brr+1,brr+1+c);
}
else {
cout<<"Heap Sort\n";
int top=brr[1];
int idx=n;
while(brr[idx]>=top)idx--;
swap(brr[idx],brr[1]);
down(1,idx-1);
}
for(int i=1;i<=n;i++){
if(i!=1)cout<<" ";
cout<<brr[i];
}
return 0;
}
标签:Sort,25,arr,cout,idx,int,brr,down,Insertion 来源: https://blog.csdn.net/Minelois/article/details/123241402