Pagini recente » Concursuri organizate de infoarena | Cod sursa (job #2671645) | Cod sursa (job #3241646) | Cod sursa (job #2704440) | Cod sursa (job #1625372)
#include<stdio.h>
#include<algorithm>
#include<ctime>
#define MAXX 500003
using namespace std;
int v[MAXX], n;
void read()
{
int i;
scanf("%d", &n);
for(i = 1; i <= n; i++)
scanf("%d", &v[i]);
}
int partitionpos(int low, int high)
{
int i, j, pivot;
pivot = v[low + rand() % high];
i = low - 1;
j = high + 1;
while (i < j) {
while (v[++i] < pivot);
while (v[--j] > pivot);
if (i < j)
swap(v[i], v[j]);
else
return j;
}
}
void quicksort(int low, int high)
{
if(low < high)
{
int pivot = partitionpos(low, high);
quicksort(low, pivot);
quicksort(pivot + 1, high);
}
}
void afis()
{
int i;
for(i = 1; i <= n; i++)
printf("%d ", v[i]);
}
int main()
{
freopen("algsort.in", "r", stdin);
freopen("algsort.out", "w", stdout);
srand(time(0));
read();
quicksort(1, n);
afis();
fclose(stdin);
fclose(stdout);
return 0;
}