Pagini recente » Cod sursa (job #2227636) | Cod sursa (job #3164848) | Cod sursa (job #3151198) | Cod sursa (job #365029) | Cod sursa (job #3128853)
#include <iostream>
#include <fstream>
using namespace std;
int v[500000];
int quicksort1(int start, int final)
{
int pivot = v[(start+final)/2];
int index = start - 1;
for (int i = start; i < final ; i++)
{
if (v[i] < pivot)
{
index++;
int aux = v[index];
v[index] = v[i];
v[i] = aux;
}
}
int aux = v[index + 1];
v[index + 1] = v[final];
v[final] = aux;
return index + 1;
}
void quicksort(int start, int final) {
if (final < start)
return;
int felie = quicksort1(start, final);
quicksort(start, felie - 1);
quicksort(felie + 1, final);
}
int main() {
ifstream in("algsort.in");
ofstream out("algsort.out");
int n;
in >> n;
for (int i = 0; i < n; i++)
in >> v[i];
quicksort(0, n - 1);
for (int i = 0; i < n; i++)
out << v[i]<<" ";
}