Pagini recente » Cod sursa (job #2378408) | Cod sursa (job #924694) | Cod sursa (job #1200171) | Cod sursa (job #1522408) | Cod sursa (job #2822816)
#include <fstream>
#include <algorithm>
#include <vector>
#define ll long long
using namespace std;
const int NMAX = 500003;
ll v[NMAX];
ifstream cin("algsort.in");
ofstream cout("algsort.out");
void quicksort(int st, int dr)
{
if (st >= dr)
return ;
int pivot = (st + dr) / 2, x1 = st, x2 = dr;
while (x1 <= x2)
{
if (v[x1] >= v[pivot])
{
if (v[x2] > v[pivot])
x2--;
else
{
if (x1 == pivot)
pivot = x2;
else if (x2 == pivot)
pivot = x1;
swap(v[x1], v[x2]);
x1++;
x2--;
}
}
else
{
if (v[x2] < v[pivot])
x1++;
else
{
x2--;
x1++;
}
}
}
/*cout << "Prima partitie: ";
for (int i = st; i <= pivot - 1; i++)
cout << v[i] << " ";
cout << "\nA doua partitie: ";
for (int i = pivot + 1; i <= dr; i++)
cout << v[i] << " ";
cout << "\n\n\n";*/
quicksort(st, pivot - 1);
quicksort(pivot + 1, dr);
}
int main()
{
int i, n;
cin >> n;
for (i = 1; i <= n; i++)
cin >> v[i];
/*int pivot = (1 + n) / 2, st = 1, dr = n;
while (st <= dr)
{
if (v[st] >= v[pivot])
{
if (v[dr] > v[pivot])
dr--;
else
{
if (st == pivot)
pivot = dr;
swap(v[st], v[dr]);
st++;
}
}
else
{
if (v[dr] < v[pivot])
st++;
else
{
dr--;
st++;
}
}
}*/
quicksort(1, n);
for (i = 1; i <= n; i++)
cout << v[i] << " ";
//cout << "\n" << pivot;
}