Pagini recente » Cod sursa (job #2729089) | Cod sursa (job #2730021) | Cod sursa (job #1531862) | Cod sursa (job #944132) | Cod sursa (job #1712585)
#include<fstream>
#define MAXN 500005
using namespace std;
int A[MAXN];
int modul(int a, int b) {
int c = a / b;
return a - b * c;
}
int partition(int left, int right) {
int pivotPos = modul(rand(), (right - left + 1));
int aux = A[left];
A[left] = A[left + pivotPos];
A[left + pivotPos] = aux;
int pos = left;
for (int i = left + 1; i <= right; i++) {
if (A[i] < A[left]) {
pos++;
aux = A[i];
A[i] = A[pos];
A[pos] = aux;
}
}
aux = A[left];
A[left] = A[pos];
A[pos] = aux;
return pos;
}
void qsort(int left, int right) {
if (left < right) {
int pos = partition(left, right);
qsort(left, pos - 1);
qsort(pos + 1, right);
}
}
int main() {
int n;
srand(time(0));
ios_base::sync_with_stdio(0);
ifstream f("algsort.in");
ofstream g("algsort.out");
f >> n;
for (int i = 1; i <= n; i++) {
f >> A[i];
}
qsort(1, n);
for (int i = 1; i <= n; i++) {
g << A[i] << " ";
}
g << endl;
return 0;
}