Pagini recente » Cod sursa (job #3288706) | Cod sursa (job #2911337) | Cod sursa (job #1759139)
#include <iostream>
#include <fstream>
#include <algorithm>
#include <cstdlib>
#include <ctime>
#define nmax 500005
using namespace std;
ifstream f("algsort.in");
ofstream g("algsort.out");
int n;
int A[nmax];
void read() {
f>>n;
for (int i=1;i<=n;++i)
f>>A[i];
f.close();
}
void sort(int left, int right) {
// cout<<left<<' '<<right<<endl;
int mij = rand() % right + 1;
int pivot = A[mij];
int i = left;
int j = right;
while (left < j || i < right) {
while (A[i] < pivot)
i++;
while (A[j] > pivot)
j--;
if (i <= j) {
// cout<<"Swapping "<<A[i]<<' '<<A[j]<<endl;
swap(A[i], A[j]);
i++;
j--;
} else {
if (left < j)
sort(left, j);
if (i < right)
sort(i, right);
return;
}
}
}
void output() {
for (int i=1;i<n;++i)
g<<A[i]<<' ';
g<<A[n]<<'\n';
}
int main() {
read();
srand(time(NULL));
sort(1, n);
output();
g.close();
return 0;
}