Cod sursa(job #2589851)

Utilizator segtreapMihnea Andreescu segtreap Data 27 martie 2020 00:30:49
Problema Sortare prin comparare Scor 80
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.85 kb

#include <algorithm>
#include <cstdio>
#include <vector>
//#include <random>

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;
}

//mt19937 rng(228);

vector<int> solve(vector<int> a) {
	//shuffle(a.begin(), a.end(), rng);
	int n = (int) a.size();
	bool changes = true;
	while (changes) {
		changes = false;
		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;
				}
			}
		}
	}
	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()));
}