Cod sursa(job #379267)

Utilizator juniorOvidiu Rosca junior Data 31 decembrie 2009 13:11:07
Problema Sortare prin comparare Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.61 kb
#include <fstream>

using namespace std;

int a[500001], n, i;

void qs(int p, int u) {
  int st, dr, x, y;
   
  st = p; dr = u;
  x = a[(p+u)/2];
  do {
    while (a[st] < x)
     st++;
    while (a[dr] > x)
     dr--;
    if (st <= dr) {
      y = a[st]; a[st] = a[dr]; a[dr] = y; st++; dr--;
    }
  } while (st <= dr);
   if (dr > p)
     qs(p, dr);
   if (st < u)
     qs(st, u);
}
int main () {
  ifstream fi ("algsort.in");
  ofstream fo ("algsort.out");
  fi >> n;
  for (i = 1; i <= n; i++)
    fi >> a[i];
  qs(1, n);
  for (i = 1; i <= n; i++)
    fo << a[i] << ' ';
  return 0;
}