Pagini recente » Cod sursa (job #3223198) | Cod sursa (job #2461167) | Cod sursa (job #3256488) | Cod sursa (job #3255322) | Cod sursa (job #2739237)
#include <fstream>
#include <cstdlib>
#define NMAX 500005
using namespace std;
int v[NMAX], n, i;
int get_partition(int v[], int left, int right, int pivot)
{
while(left <= right)
{
while(v[left] < pivot)
left ++;
while(v[right] > pivot)
right --;
if(left <= right)
{
swap(v[left], v[right]);
left ++;
right --;
}
}
return left;
}
void QSORT(int v[], int left, int right)
{
if(left < right)
{
int random = left + rand() % (right - left);
int pivot = v[random];
int index = get_partition(v, left, right, pivot);
QSORT(v, left, index - 1);
QSORT(v, index, right);
}
else
return;
}
int main()
{
ifstream f("algsort.in");
ofstream g("algsort.out");
f >> n;
for(i = 0; i < n; ++i)
f >> v[i];
QSORT(v, 0, n - 1);
for(i = 0; i < n; ++i)
g << v[i] << " ";
f.close();
g.close();
return 0;
}