Cod sursa(job #1691220)

Utilizator petru.cehanCehan Petru petru.cehan Data 17 aprilie 2016 14:46:01
Problema Sortare prin comparare Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.88 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin("algsort.in");
ofstream fout("algsort.out");

int n,el;
vector <int> nums;

void Read(){
    fin >> n;
    for(int i = 1; i <= n; ++ i){
        fin >> el;
        nums.push_back(el);
    }
}

void quick_sort(vector<int> &arr,int low,int high)
{
 srand(time(NULL));
 int pivot,j,i;
 if(low<high)
 {
  pivot = low + rand() % (high-low);
  i = low;
  j = high;

  while(i<j)
  {
   while((arr[i]<=arr[pivot])&&(i<high))
   {
    i++;
   }

   while(arr[j]>arr[pivot])
   {
    j--;
   }

   if(i<j)
   {
    swap(arr[i],arr[j]);
   }
  }

  swap(arr[j],arr[pivot]);
  quick_sort(arr,low,j-1);
  quick_sort(arr,j+1,high);
 }
}
int main()
{
  Read();
  quick_sort(nums,0,nums.size()-1);
  for(unsigned int i =0; i < nums.size() ; ++ i){
    fout << nums[i] << " ";
  }
  return 0;
}