Pagini recente » Cod sursa (job #1556932) | Cod sursa (job #2776241) | Cod sursa (job #2359063) | Cod sursa (job #2692150) | Cod sursa (job #1625049)
#include<stdio.h>
#include<algorithm>
#include<cstdio>
#include<ctime>
#define maxn 3000005
using namespace std;
int n,k;
int a[maxn];
void read()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
}
int position(int left,int right)
{
int i=left,j=right;
int pivot=rand()%(right-left)+left;
swap(a[left],a[pivot]);
while(i<j)
{
while(a[i]<=a[j] && i<j)
j--;
if(i<j)
swap(a[i],a[j]),i++;
while(a[i]<=a[j] && i<j)
i++;
if(i<j)
swap(a[i],a[j]),j--;
}
return i;
}
void quick(int left,int right)
{
if(left==right) return;
int p=position(left,right);
if(k<p) quick(left,p-1);
else
if(k>p) quick(p+1,right);
}
int main()
{
freopen("algsort.in","r",stdin);
freopen("algsort.out","w",stdout);
srand(time(0));
read();
quick(1,n);
//printf("%d",a[k]);
for(i = 1; i <= n; i++)
printf("%d ", a[i]);
fclose(stdin);
fclose(stdout);
return