Pagini recente » Cod sursa (job #3265486) | Cod sursa (job #1964188) | Cod sursa (job #154862) | Cod sursa (job #2751893) | Cod sursa (job #2618635)
#include <cstdio>
#include <algorithm>
#include <iostream>
#include <queue>
#include <cstdlib>
#include <ctime>
using namespace std;
const int NMAX = 500505;
int N, A[NMAX];
void sortArray(int A[], int from, int to) {
if (to - from <= 0) {
return;
}
int pivotIdx = from + rand() % (to - from + 1);
int elemsLtePivot = 0;
for (int i = from; i <= to; i++) {
if (A[i] <= A[pivotIdx]) {
elemsLtePivot++;
}
}
int newPivotIdx = from + elemsLtePivot - 1;
swap(A[pivotIdx], A[newPivotIdx]);
int nextAvailablePos = from;
for (int i = from; i <= to; i++) {
if (i == newPivotIdx) {
continue;
} else if (A[i] <= A[newPivotIdx]) {
swap(A[i], A[nextAvailablePos++]);
}
}
sortArray(A, from, newPivotIdx - 1);
sortArray(A, newPivotIdx + 1, to);
}
int main() {
freopen("algsort.in", "r", stdin);
freopen("algsort.out", "w", stdout);
srand(time(0));
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> N;
for (int idx = 1; idx <= N; idx++) {
cin >> A[idx];
}
sortArray(A, 1, N);
for (int idx = 1; idx <= N; idx++) {
if (idx > 1) {
cout << " ";
}
cout << A[idx];
}
cout << "\n";
return 0;
}