Cod sursa(job #2624337)

Utilizator roxana1708Roxana Gherghina roxana1708 Data 4 iunie 2020 18:40:29
Problema Statistici de ordine Scor 50
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.94 kb
#include <fstream>
#include <stdlib.h>

using namespace std;

#define maxn 3000001
int n, k, i, j, ind_pivot;
long long int v[maxn], pivot;

void swap(long long int *x, long long int *y){
    int temp = *x;
    *x = *y;
    *y = temp;
}

long long int partition(int l, int r){

    pivot = v[l + (rand() % (r - l))];

    i = l; j = r;

    while(i <= j) {
        while(v[i] < pivot)
            i++;
        while(v[j] > pivot)
            j--;
        if(i <= j) {
            swap(&v[i], &v[j]);
            i++;
            j--;
        }
    }

    return i;
}

void quickSort(long long int st, long long int dr){
    ind_pivot = partition(st, dr);

    if(st < ind_pivot-1)
        quickSort(st, ind_pivot - 1);

    if(ind_pivot < dr)
        quickSort(ind_pivot, dr);
}

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

    f >> n >> k;

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

    quickSort(1, n);

    g << v[k];
    return 0;
}