Cod sursa(job #1654654)

Utilizator andreinmAndrei andreinm Data 17 martie 2016 12:32:39
Problema Statistici de ordine Scor 60
Compilator cpp Status done
Runda Arhiva educationala Marime 0.94 kb
#include <cstdio>
#include <iostream>
using namespace std;

#define FILE_in "sdo.in"
#define FILE_out "sdo.out"

unsigned n, k;
unsigned v[4000005];

void quickSort(unsigned v[], unsigned left, unsigned right) {
      unsigned i = left, j = right;
      unsigned pivot = v[(left + right) / 2];

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

      if (left < j)
            quickSort(v, left, j);
      if (i < right)
            quickSort(v, i, right);
}

int main()
{
    freopen(FILE_in, "r", stdin);
    freopen(FILE_out, "w", stdout);

    scanf ("%u %u", &n, &k);
    for (int i = 1; i <= n; ++i)
        scanf ("%u", &v[i]);

    quickSort(v, 1, n);

    printf ("%u\n", v[k]);

    return 0;
}