Pagini recente » Cod sursa (job #1491805) | Cod sursa (job #576000) | Cod sursa (job #1416782) | Cod sursa (job #768297) | Cod sursa (job #3245779)
#include <fstream>
using namespace std;
ifstream fin("algsort.in");
ofstream fout("algsort.out");
#define N_MAX 500000
void swap(int &a, int &b) {
const int temp = a;
a = b;
b = temp;
}
int partition(int a[], int l, int r) {
int mid = (l + r) / 2;
swap(a[mid], a[r]);
int pivot = a[r];
int i = l;
for (int j = l; j < r; j++) {
if (a[j] < pivot) {
swap(a[j], a[i]);
++i;
}
}
swap(a[i], a[r]);
return i;
}
void quick_sort(int a[], const int l, const int r) {
const int pivot = partition(a, l, r);
if (l < pivot - 1) {
quick_sort(a, l, pivot - 1);
}
if (pivot + 1 < r) {
quick_sort(a, pivot + 1, r);
}
}
int main() {
int n;
fin >> n;
int a[N_MAX];
for (int i = 0; i < n; ++i) {
fin >> a[i];
}
quick_sort(a, 0, n - 1);
for (int i = 0; i < n; ++i) {
fout << a[i] << " ";
}
return 0;
}