Cod sursa(job #1460755)

Utilizator glbglGeorgiana bgl glbgl Data 13 iulie 2015 20:23:26
Problema Sortare prin comparare Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.8 kb
#include <stdio.h>
#include <stdlib.h>
#include <fstream>
#include <vector>
#include <algorithm>
 
using namespace std;
 
ifstream in("algsort.in");
ofstream out("algsort.out");
 
int N;
vector<int> v;
 
void read(){
 
    in >> N;
    v.resize(N);
    int x;
    for(int i = 0; i < N; ++i){
        in >> x;
        v[i] = x;
    }
}
 
void Quicksort(int l, int r){

	
	if(l < r){

		int pivot = v[l + (r - l)/2];
		int i = l, j = r;

		while(i < j){
			while(i < r && v[i] < pivot)
				i++;
			while(j > l && v[j] > pivot)
				j--;
			if(i <= j){
				swap(v[i],v[j]);
				i++;
				j--;
			}
		}

		Quicksort(l,j);
		Quicksort(i,r);
	}
}
 
void write(){
 
    for(int i = 0; i < N; ++i){
        out << v[i] << " ";
    }
}
 
 
int main(){
 
 	ios::sync_with_stdio(false);
    read();
    Quicksort(0, N-1);
    write();
}