Cod sursa(job #522709)

Utilizator bocacristiBoca Nelu Cristian bocacristi Data 15 ianuarie 2011 22:14:38
Problema Sortare prin comparare Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.79 kb
#include <fstream>
using namespace std;


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

void quickSort(int v[], int s, int d);

int main()
{
	int a[500100], n;
	fin >> n;
	
	for(int i = 1; i <= n; i++ )
		fin >> a[i];
	
	quickSort(a, 0, n);
	
	for ( int i = 1; i <= n; i++ )
		fout << a[i] << ' ';
	
	fin.close();
	fout.close();
}

void quickSort(int v[], int s, int d) 
{  
int i = s, j = d;  
int tmp;  
int pivot = v[(s + d) / 2];  
while (i <= j) {  
    while (v[i] < pivot)  
        i++;  
    while (v[j] > pivot)  
        j--;  
    if (i <= j)
	{  
		tmp = v[i];  
        v[i] =v[j];  
        v[j] = tmp;  
        i++;  
        j--;  
    }  
    };  
    if (s < j)  
		quickSort(v, s, j);  
	if (i < d)  
		quickSort(v, i, d);  
}