Pagini recente » Cod sursa (job #1789489) | Cod sursa (job #1865262) | Cod sursa (job #1815996)
#include <iostream>
#include <fstream>
#define NMAX 3000001
#define BUFF_SIZE 100001
using namespace std;
ifstream in("algsort.in");
ofstream out("algsort.out");
int A[NMAX], n, k, pos = 0;
char buff[BUFF_SIZE];
void Read(int &a)
{
while (!isdigit(buff[pos]))
if (++pos == BUFF_SIZE)
in.read(buff, BUFF_SIZE), pos = 0;
a = 0;
while (isdigit(buff[pos]))
{
a = a * 10 + (buff[pos] - '0');
if (++pos == BUFF_SIZE)
in.read(buff, BUFF_SIZE), pos = 0;
}
}
void quicksort(int x, int y)
{
if (x < y)
{
int i = x, j = y, pivot = A[(x + y)/2];
while (i <= j)
{
while (A[i] < pivot)
i++;
while (A[j] > pivot)
j--;
if (i <= j)
{
swap(A[i], A[j]);
i++;
j--;
}
}
if (i <= k && k <= y)
quicksort(i, y);
if (j >= k && k >= x)
quicksort(x, j);
}
}
int main()
{
Read(n);
Read(k);
for (int i = 1; i <= n; i++)
Read(A[i]);
in.close();
quicksort(1, n);
out << A[k] << "\n";
out.close();
return 0;
}