Pagini recente » Cod sursa (job #387507) | Cod sursa (job #1620126) | Cod sursa (job #245469) | Cod sursa (job #403569) | Cod sursa (job #1198084)
#include<fstream>
#include<cstdlib>
using namespace std;
ifstream fin("algsort.in");
ofstream fout("algsort.out");
int n,a[500005];
inline int Partition(const int st,const int dr)
{
int k,c,i=st,j=dr,x,storeindex;
c=(st+dr)>>1;
x=a[c];
swap(a[c],a[dr]);
j--;storeindex=i;
for (k=i;k<=j;k++)
if (a[k]<=x)
{
swap(a[k],a[storeindex]);
storeindex++;
}
swap(a[storeindex],a[dr]);
return storeindex;
}
inline void Qsort(const int st, const int dr)
{
int mij=Partition(st,dr);
if (mij-1>st) Qsort(st,mij-1);
if (mij+1<dr) Qsort(mij+1,dr);
}
int main()
{
int i;
fin>>n;
for (i=1;i<=n;i++) fin>>a[i];
Qsort(1,n);
for (i=1;i<=n;i++) fout<<a[i]<<" ";
return 0;
}