Pagini recente » Cod sursa (job #793427) | Cod sursa (job #676511) | Cod sursa (job #384130) | Cod sursa (job #1549023) | Cod sursa (job #2249685)
#include <bits/stdc++.h>
using namespace std;
ifstream f("algsort.in"); ofstream g("algsort.out");
int n;
int v[500005];
void Read()
{
f >> n;
for(int i = 1; i <= n; ++i)
f >> v[i];
}
int Random(int left, int right)
{
srand(time(NULL));
return left + rand() % (right - left);
}
void QuickSort(int left, int right)
{
int low = left, high = right;
//int pivot = v[Random(left, right)];
int pivot = v[(left + right) / 2];
do
{
while(low < right && v[low] < pivot)
++low;
while(high > left && v[high] > pivot)
--high;
if(low <= high)
{
swap(v[low], v[high]);
++low;
--high;
}
}while(low <= high);
if(left < high)
QuickSort(left, high);
if(low < right)
QuickSort(low, right);
}
void Print()
{
for(int i = 1; i <= n; ++i)
g << v[i] << " ";
g << "\n";
}
int main()
{
Read();
QuickSort(1, n);
Print();
return 0;
}