Cod sursa(job #2615961)

Utilizator avtobusAvtobus avtobus Data 15 mai 2020 23:25:26
Problema Sortare prin comparare Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.93 kb
#include <stdio.h>
#include <bits/stdc++.h>

#define rep(i, n) for(int i = 0; i < (int)(n); i++)

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
const int INF = 0x3f3f3f3f;

ifstream fin ("algsort.in");
ofstream fout ("algsort.out");
const int Nmax = 500555;
int a[Nmax];

void mqsort(int l, int r) {
  if (l >= r) { return; }

  int m = l + rand() % (r-l+1);
  int pivot = a[m];
  int i = l, j = r;
  while(i <= j) {
    while(a[i] < pivot)
      ++i;
    while(a[j] > pivot)
      --j;
    if(i <= j) {
      swap(a[i], a[j]);
      ++i;
      --j;
    }
  }
  if (i < r) {
    mqsort(i, r);
  }
  if (l < j) {
    mqsort(l, j);
  }
}
int main(void) {
  // freopen("algsort.in", "r", stdin);
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(NULL);
  int n;
  fin >> n;
  rep(i, n) {
    fin >> a[i];
  }

  mqsort(0, n-1);

  rep(i, n) {
    fout << a[i] << " ";
  }
  fout << "\n";

  return 0;
}