Pagini recente » Cod sursa (job #2254822) | Cod sursa (job #931961) | Cod sursa (job #3235681) | Cod sursa (job #3233296) | Cod sursa (job #608180)
Cod sursa(job #608180)
#include <fstream.h>
#include <iostream.h>
#define MAX 500001
#define MAXX 2000
ifstream f("algsort.in");
ofstream g("algsort.out");
int v[MAX],s[MAXX];
int partition(int a, int b);
int main()
{
int i,n,c=0,stop,start,j,p,aux,k,end;
f>>n;
for(i=1;i<=n;i++)
f>>v[i];
s[c++]=1;
s[c++]=n;
while(c>0)
{
stop=s[--c], start=s[--c];
if(start>=stop)
continue;
i=partition(start, stop);
if(i-start > stop-i)
{
s[c++]=i+1, s[c++]=stop;
s[c++]=start, s[c++]=i-1;
}
else
{
s[c++]=i+1, s[c++]=stop;
s[c++]=start, s[c++]=i-1;
}
}
for(i=1;i<=n;i++)
g<<v[i]<<" ";
f.close();
g.close();
return 0;
}
int partition(int start, int stop)
{
int i=start, j=stop, p=v[(start+stop)/2],aux;
while(true)
{
while(v[i]<p)
i++;
while(v[j]>p)
j--;
if(i<j)
aux=v[i], v[i]=v[j], v[j]=aux;
else
return j;
}
return i;
}