Cod sursa(job #2043885)

Utilizator mmdretroMihai Dretcanu mmdretro Data 20 octombrie 2017 18:37:25
Problema Statistici de ordine Scor 50
Compilator cpp Status done
Runda Arhiva educationala Marime 1.13 kb
#include <iostream>
#include <fstream>

using namespace std;

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

unsigned long v[4100000], n, k, V_K;

void citire(unsigned long& n, unsigned long& k)
{
    unsigned long i;
    fin>>n>>k;
    for(i=1;i<=n;i++)
    {
        fin>>v[i];
    }
}

void quicksort(unsigned long x1, unsigned long x2,unsigned long& V_K)
{
    unsigned long i, j, aux, pivot;
    i=x1;
    j=x2;
    pivot=v[(x1+x2)/2];
    while(i<=j)
    {
        while(v[i]<pivot)
        {
            i=i+1;
        }
        while(v[j]>pivot)
        {
            j=j-1;
        }
        if(i<=j)
        {
            aux=v[i];
            v[i]=v[j];
            v[j]=aux;
            i=i+1;
            j=j-1;
        }
    }
    if(k==j+1)
    {
        V_K=v[k];
    }
    else
    {
        if(k<j+1)
        {
            quicksort(x1, j, V_K);
        }
        else
        {
            quicksort(i, x2, V_K);
        }
    }
}
void afisare(unsigned long V_k)
{
    fout<<V_k;
}
int main()
{
    citire(n, k);
    quicksort(1, n, V_K);
    afisare(V_K);
}