Pagini recente » Cod sursa (job #1866403) | Cod sursa (job #1803735) | Cod sursa (job #1384004) | Cod sursa (job #580340) | Cod sursa (job #2589797)
#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;
const int N = 500000 + 7;
int n;
int a[N];
int main() {
freopen ("algsort.in", "r", stdin);
freopen ("algsort.out", "w", stdout);
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
}
int x = 1;
while (x * 2 <= n) {
x *= 2;
}
x = 1;
while (x <= n) {
for (int i = n - x + 1; i >= 1; i--) {
if (a[i] > a[i + x - 1]) {
swap(a[i], a[i + x - 1]);
}
}
for (int i = 1; i <= n - x + 1; i++) {
if (a[i] > a[i + x - 1]) {
swap(a[i], a[i + x - 1]);
}
}
x *= 2;
}
x = 1;
while (x <= n) {
for (int i = n - x + 1; i >= 1; i--) {
if (a[i] > a[i + x - 1]) {
swap(a[i], a[i + x - 1]);
}
}
for (int i = 1; i <= n - x + 1; i++) {
if (a[i] > a[i + x - 1]) {
swap(a[i], a[i + x - 1]);
}
}
x *= 2;
}
for (int i = 1; i <= n; i++) {
printf("%d ", a[i]);
}
printf("\n");
}