Pagini recente » Cod sursa (job #1813860) | Cod sursa (job #386754) | Cod sursa (job #2119246) | Cod sursa (job #2719400) | Cod sursa (job #1605489)
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
const int N = 500000;
ifstream in("algsort.in");
ofstream out("algsort.out");
int n, v[N];
inline void schimb(int &x, int &y)
{
int aux = x;
x = y;
y = aux;
}
void citire(int v[], int &n)
{
in >> n;
for (int i = 0; i < n; i++)
in >> v[i];
in.close();
}
void amestecare(int v[], int n)
{
int i, j;
for (i = n - 1; i >= 0; i--)
{
j = rand() % (1 + i);
schimb(v[i], v[j]);
}
}
int pivotare(int v[], int st, int dr)
{
int i, j = st - 1;
for (i = st; i < dr; i++)
if (v[i] < v[dr])
schimb(v[i], v[++j]);
schimb(v[i], v[++j]);
return j;
}
void sortare(int v[], int st, int dr)
{
if (st >= dr)
return;
int p = pivotare(v, st, dr);
sortare(v, st, p - 1);
sortare(v, p + 1, dr);
}
void afisare(int v[], int n)
{
out << v[0];
for (int i = 1; i < n; i++)
out << " " << v[i];
out << "\n";
out.close();
}
int main()
{
citire(v, n);
amestecare(v, n);
sortare(v, 0, n - 1);
afisare(v, n);
return 0;
}