Pagini recente » Cod sursa (job #250214) | Borderou de evaluare (job #676811) | Cod sursa (job #2345387) | Cod sursa (job #1103233) | Cod sursa (job #1193626)
#include<fstream>
using namespace std;
int n, v[500003], c, p, i, aux;
ifstream in("algsort.in");
ofstream out("algsort.out");
int main(){
in>>n;
in>>v[1];
for(i=2; i<=n; i++){
in>>v[i];
c=i;
p=i/2;
while(p>0){
if(v[p]<v[c]){
aux=v[p];
v[p]=v[c];
v[c]=aux;
}
else
break;
c=p;
p=p/2;
}
}
for(i=n; i>=2; i--){
aux=v[1];
v[1]=v[i];
v[i]=aux;
//corectez heapul cu i-1 elemente afectat de radacina
p=1;
c=2*p;
while(c<=i-1){
if(c+1<=i-1 && v[c+1]>v[c])
c++;
if(v[p]<v[c]){
aux=v[p];
v[p]=v[c];
v[c]=aux;
}
else
break;
p=c;
c=2*c;
}
}
for(i=1; i<=n; i++)
out<<v[i]<<" ";
return 0;
}