Cod sursa(job #2589855)

Utilizator segtreapMihnea Andreescu segtreap Data 27 martie 2020 00:35:57
Problema Sortare prin comparare Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.11 kb
#include <algorithm>
#include <cstdio>
#include <vector>

using namespace std;

vector<int> read() {
  int n;
  scanf("%d", &n);
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
  }
  return a;
}

vector<int> solve(vector<int> a) {
  int n = (int) a.size();
  bool changes = true;
  bool state = 0;
  while (changes) {
    changes = false;
    if (state) {
      for (int x = n - 1; x > 0; x /= 2) {
        for (int i = n - x - 1; i >= 0; i--) {
          if (a[i + x] < a[i]) {
            swap(a[i], a[i + x]);
            changes = true;
          }
        }
      }
    } else {
      for (int x = n - 1; x > 0; x /= 2) {
        for (int i = 0; i + x < n; i++) {
          if (a[i + x] < a[i]) {
            swap(a[i], a[i + x]);
            changes = true;
          }
        }
      }
    }
    state ^= 1;
  }
  return a;
}

void print(vector<int> a) {
  for (auto &x : a) {
    printf("%d ", x);
  }
  printf("\n");
}

int main() {
  freopen ("algsort.in", "r", stdin);
  freopen ("algsort.out", "w", stdout);
  print(solve(read()));
}