Pagini recente » Cod sursa (job #2798436) | Cod sursa (job #980125) | Cod sursa (job #822653) | Cod sursa (job #254421) | Cod sursa (job #2988183)
#include <bits/stdc++.h>
using namespace std;
ifstream in("algsort.in");
ofstream out("algsort.out");
int n,a[500100];
void quick_sort(int l, int r, int v[])
{
srand(time(NULL));
int pos = l + (rand() % (r - l + 1));
int piv = v[pos];
int i = l, j = r;
while (i <= j) {
while (a[i] < piv) i++;
while (a[j] > piv) j--;
if (i <= j)
swap(a[i], a[j]), i++, j--;
}
if (i < r)
quick_sort(i, r, v);
if (j > l)
quick_sort(l, j, v);
}
int main()
{
in>>n;
for (int i = 1; i <= n; i++)
in>>a[i];
quick_sort(1, n, a);
for (int i = 1; i <= n; i++)
out<<a[i]<<" ";
out<<'\n';
return 0;
}