Pagini recente » Cod sursa (job #2947746) | Cod sursa (job #561661) | Cod sursa (job #1470014) | Cod sursa (job #1726755) | Cod sursa (job #1276648)
#include <cstdio>
using namespace std;
int a[500004], n;
inline void Sch(int &x, int &y)
{
int aux;
aux = x;
x = y;
y = aux;
}
inline int Pivot(int st, int dr)
{
int i, j, piv;
piv = a[st];
i = st + 1;
j = dr;
while (i <= j)
{
if (a[i] <= piv) i++;
if (a[j] >= piv) j--;
if (i < j && a[i] > piv && piv > a[j])
{
Sch(a[i], a[j]);
i++;
j--;
}
}
Sch(a[i-1], a[st]);
return i - 1;
}
inline void QuickSort(int st, int dr)
{
int m;
m = Pivot(st, dr);
if (st < m - 1) QuickSort(st, m - 1);
if (dr > m + 1) QuickSort(m + 1, dr);
}
int main()
{
int i;
freopen("algsort.in", "r", stdin);
scanf("%d", &n);
for (i = 1; i <= n; i++)
scanf("%d", &a[i]);
QuickSort(1, n);
freopen("algsort.out", "w", stdout);
for (i = 1; i <= n; i++)
printf("%d ", a[i]);
return 0;
}