Cod sursa(job #1555250)

Utilizator radoneNeacsu Radu-Stefan radone Data 22 decembrie 2015 14:39:55
Problema Statistici de ordine Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.99 kb
#include <iostream>
#include <time.h>
#include <stdlib.h>
#include <fstream>
using namespace std;

ifstream f("sdo.in");
ofstream g("sdo.out");

int v[3000030], n, k;

int partitie( int s, int d )
{
    int p = s + rand()%(d - s + 1);
    int i, j, x, aux;
    aux = v[p];
    v[p] = v[d];
    v[d] = aux;
    i = s - 1;
    x = v[d];
    for( j = s; j < d; j ++ )
        if(v[j] <= x )
        {
            i ++;
            aux = v[i];
            v[i] = v[j];
            v[j] = aux;
        }
    aux = v[i+1];
    v[i+1] = v[d];
    v[d] = aux;
    return i+1;
}

void quick( int s, int d )
{
    if( s < d )
    {
        int q = partitie( s, d );
        if( q == k )
            return;
        else if( q < k )
            quick( q+1, d );
        else if( q > k )
        quick( s, q-1 );
    }
}

int main()
{
    f>>n>>k;

    for(int i = 1; i <= n; i ++ )
        f>>v[i];
    srand( time(NULL) );
    quick(  1, n );


    g<<v[k];
}