Cod sursa(job #290631)

Utilizator AstronothingIulia Comsa Astronothing Data 28 martie 2009 14:23:58
Problema Sortare prin comparare Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.74 kb
#include <fstream>

using namespace std;

void quicksort(int a[],int li,int lf)
{
    if(li<lf)
    {
    int ind = (li+lf)/2;
    int i=li,j=lf;
    while(i<=j)
    {
        while(a[ind]>a[i]) i++;
        while(a[ind]<a[j]) j--;

        if(i<=j)
        {
            int aux = a[i];
            a[i] = a[j];
            a[j] = aux;
            i++;
            j--;
        }
    }

    if(li<j) quicksort(a,li,j);
    if(lf>i) quicksort(a,i,lf);
    }
}

int main()
{
    ifstream f("algsort.in");
    ofstream f2("algsort.out");
    int n,a[500001];
    f>>n;
    for(int i=0;i<n;i++) f>>a[i];

    quicksort(a,0,n-1);

    for(int i=0;i<n;i++) f2<<a[i]<<" ";

    f.close();
    f2.close();
    return 0;
}