Cod sursa(job #1018073)

Utilizator bghimisFMI Ghimis Bogdan bghimis Data 28 octombrie 2013 20:46:07
Problema Sortare prin comparare Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.99 kb
#include <fstream>
using namespace std;

int v[500000];

void swap (int i, int j)
{
  int tmp = v[i];
  v[i] = v[j];
  v[j] = tmp;
}

void qs (int left, int right)
{
  //cout << left << " " << right << "\n";

  if (right - left <= 1) 
    return;

  int pivot = left;

  int l = left, r = right;
  while (l <= r)
    {
      //cout << l << " " << r << endl;

      while (v[l] < v[pivot])
	l++;
      while (v[r] > v[pivot])
	r--;

      if (l <= r)
	{
	  swap (l, r);
	  l++;
	  r--;
	}
    }
  
  qs(left, pivot - 1);
  qs(pivot, right);
}

int main()
{   
  ifstream cin("algsort.in");
  ofstream cout("algsort.out");
  
  int n;
  cin >> n;

  cout << "OMG am citit n" << "\n"; 

  for(int i = 0; i < n; i++)
    {
      cin >> v[i];
      //cout << "LOL CITIT: " << v[i] << endl;
    }

  //cout << "OMG chiar si vectorul" << "\n";

  qs(0, n - 1);

  for(int i = 0; i < n; i++)
    cout << v[i] << " ";
    
  cin.close();
  cout.close();
}