Cod sursa(job #2589802)

Utilizator segtreapMihnea Andreescu segtreap Data 26 martie 2020 21:40:41
Problema Sortare prin comparare Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.68 kb
#include <algorithm>
#include <cstdio>
#include <iostream>

using namespace std;

const int N = 500000 + 7;
int n;
int a[N];

void so() {
  while (1) {
    bool ch = 0;
    for (int x = 1; x <= n; x *= 2) {
      for (int i = 1; i + x <= n; i++) {
        if (a[i] > a[i + x]) {
          swap(a[i], a[i + x]);
          ch = 1;
        }
      }
    }
    if (ch) {
      break;
    }
  }
}

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]);
  }
  so();
  for (int i = 1; i <= n; i++) {
    printf("%d ", a[i]);
  }
  printf("\n");
}