Pagini recente » Monitorul de evaluare | Cod sursa (job #2286841) | Cod sursa (job #345078) | Cod sursa (job #321778) | Cod sursa (job #2494520)
#include<fstream>
using namespace std;
ifstream fin("algsort.in");
ofstream fout("algsort.out");
int n,parinte,copil;
int heap[500001];
void sortHeap(){
for(int i=2;i<=n;i++){
copil=i;
parinte=copil/2;
while(copil!=1){
if(heap[copil]>heap[parinte]){
swap(heap[copil],heap[parinte]);
copil=parinte;
parinte/=2;
}
else
break;
}
}
for(int i=n;i>=1;i--){
swap(heap[1],heap[i]);
parinte=1;
copil=2;
while(copil<i){
if(copil+1<i && heap[copil+1]>heap[copil])
copil++;
if(heap[copil]>heap[parinte]){
swap(heap[copil],heap[parinte]);
parinte=copil;
copil*=2;
}
else
break;
}
}
return ;
}
int main(){
fin>>n;
for(int i=1;i<=n;i++)
fin>>heap[i];
sortHeap();
for(int i=1;i<=n;i++)
fout<<heap[i]<<" ";
return 0;
}