Pagini recente » Cod sursa (job #846221) | Cod sursa (job #2585696) | Cod sursa (job #826154) | Cod sursa (job #2774223) | Cod sursa (job #2906404)
#include <bits/stdc++.h>
using namespace std;
int n;
vector<int>v;
void Citire() {
ifstream f("algsort.in");
f >> n;
v.resize(n);
for(int i = 0; i < n; i++)
f >> v[i];
}
void swap(int* a, int* b)
{
int t = *a;
*a = *b;
*b = t;
}
int partition (int low, int high)
{
int pivot = low + (rand() % (high - low + 1));
int i = (low - 1);
for (int j = low; j <= high - 1; j++)
{
if (v[j] < pivot)
{
i++;
swap(&v[i], &v[j]);
}
}
swap(&v[i + 1], &v[high]);
return (i + 1);
}
void quickSort(int low, int high)
{
if (low < high)
{
int pivot = partition(low, high);
quickSort(low, pivot - 1);
quickSort(pivot + 1, high);
}
}
void Afisare()
{
ofstream g("algsort.out");
int i;
for (i = 0; i < n; i++)
g << v[i] << " ";
}
int main()
{
Citire();
quickSort(0, n - 1);
Afisare();
return 0;
}