Pagini recente » Monitorul de evaluare | Cod sursa (job #2693114) | Cod sursa (job #368840) | Cod sursa (job #485066) | Cod sursa (job #1625535)
#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 - low)];
i = low - 1;
j = high + 1;
while (i < j)
{
i++;
j--;
while (v[i] < pivot)
i++;
while (v[j] > pivot)
j--;
if (i < j)
swap(v[i], v[j]);
else
return j;
}
}
void quicksort(int low, int high)
{
int pivot;
if(low < high)
{
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;
}