Cod sursa(job #2026068)

Utilizator SlevySlevoaca Stefan-Gabriel Slevy Data 23 septembrie 2017 17:19:52
Problema Statistici de ordine Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.94 kb
#include <iostream>
#include <fstream>
#define ARRAY_SIZE 3000001

using namespace std;

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

int _array[ARRAY_SIZE];

void quicksort(int x, int y, int k) {

    if (x < y) {

        int i = x, j = y, pivot = _array[x + (y - x) / 2];

        while (i <= j) {

            while (_array[i] < pivot)
                i++;
            while (_array[j] > pivot)
                j--;
            if (i <= j) {

                swap(_array[i], _array[j]);
                i++;
                j--;
            }
        }

        if (i <= k && k <= y)
            quicksort(i, y, k);
        if (j >= k && k >= x)
            quicksort(x, j, k);
    }
}

int main()
{
    int n, k;
    in >> n >> k;

    for (int i = 1; i <= n; i++)
        in >> _array[i];
    in.close();

    quicksort(1, n, k);

    out << _array[k] << "\n";

    out.close();
    return 0;
}