Cod sursa(job #1625656)

Utilizator andreitulusAndrei andreitulus Data 2 martie 2016 20:03:49
Problema Statistici de ordine Scor 50
Compilator cpp Status done
Runda Arhiva educationala Marime 1.16 kb
#include<stdio.h>
#include<fstream>
#include<algorithm>
#include<ctime>
#define MAXX 500003
using namespace std;

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

int v[MAXX], n, k;


void read()
{
    int i;

   fin>>n>>k;

    for(i = 1; i <= n; i++)
        fin>>v[i];
}



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

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

     while (i < j)
    {
        i++;
        j--;

        while (v[i] < pivot)
            i++;

        while (v[j] > pivot)
            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);

        if(k <= pivot)
            quicksort(low, pivot);

        if(k >= pivot)
            quicksort(pivot + 1, high);
    }
}




int main()
{
    srand(time(0));

    read();

    quicksort(1, n);

    //afis();
    //printf("%d", v[k]);
    fout<<v[k];

    fin.close();
    fout.close();

    return 0;

}