Cod sursa(job #1625319)

Utilizator andreitulusAndrei andreitulus Data 2 martie 2016 18:09:15
Problema Sortare prin comparare Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.11 kb
#include<stdio.h>
#include<algorithm>
#include<ctime>
#define MAXX 500003
using namespace std;

int v[MAXX], n;


void read()
{
    int i;

    scanf("%d", &n);

    for(i = 1; i <= n; i++)
        scanf("%d", &v[i]);
}



int partitionpos(int low, int high)
{
    int i, j, pivot;

    pivot = low + rand()%(high - low);
    i = low - 1;
    j = high + 1;

    while(i < j)
    {
        while(v[pivot] > v[++i]);

        while(v[pivot] < v[--j]);

        if(i < j)
            swap(v[i], v[j]);
        else
            return j;
    }
}



void quicksort(int low, int high)
{
    int pivot;

    if(low < high)
    {
        pivot = partitionpos(low, high);
        quicksort(low, pivot);
        quicksort(pivot + 1, high);
    }
}



void afis()
{
    int i;

    for(i = 1; i <= n; i++)
        printf("%d ", v[i]);
}


int main()
{
    freopen("algsort.in", "r", stdin);
    freopen("algsort.out", "w", stdout);

    srand(time(0));

    read();

    quicksort(1, n);

    afis();

    fclose(stdin);
    fclose(stdout);
    return 0;

}