Cod sursa(job #1019443)

Utilizator Emanuel9Dumitru Emanuel Cristian Emanuel9 Data 31 octombrie 2013 08:48:20
Problema Statistici de ordine Scor 70
Compilator c Status done
Runda Arhiva educationala Marime 0.99 kb
#include "stdio.h"
#include "stdlib.h"

int v[3000000],n,poz;

int qs(int s,int d)
{
        int aux;
        int i=s-1;
        int j=d+1;
        int  pivot = v[s+rand()%(d-s+1)];

        while(s<=d){

            do{
                ++i;
            }while(v[i]<pivot);

            do{
                --j;
            }while(v[j]>pivot);
            if(i<j){
                aux=v[i];
                v[i]=v[j];
                v[j]=aux;
            }else
                return j;
        }
}

void search(int s,int d){

    if(s<d){
        int pivot = qs(s,d);
        if(pivot>=poz)
            search(s,pivot);
        else
            search(pivot+1,d);

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

    scanf("%d%d", &n,&poz);
    int i;
    for (i=1; i<=n; i++){
        scanf("%d", &v[i]);
    }
    search(1,n);
    printf("%d",v[poz]);
    fclose(stdin);
    fclose(stdout);
    return 0;
}