Cod sursa(job #1441293)

Utilizator buzu.tudor67Tudor Buzu buzu.tudor67 Data 24 mai 2015 02:10:05
Problema Sortare prin comparare Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.59 kb
#include<fstream>
using namespace std;
ifstream fi("algsort.in");
ofstream fo("algsort.out");

const int MAX_N = 500005;

int i,n,a[MAX_N];

void quicksort(int st, int dr){
	 int i = st;
	 int j = dr;
	 int pivot = a[(st+dr)/2];
	 
	 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<dr) quicksort(i,dr);
	 if(st<j) quicksort(st,j);
}

int main(){
	fi>>n;
	for(i=1;i<=n;i++) fi>>a[i];
	
	quicksort(1,n);
	
	for(i=1;i<=n;i++) fo<<a[i]<<" ";
	
	fi.close();
	fo.close();
	return 0;
}