7-171 The kth smallest element (20 points)
Given an unordered integer array of size n (1≤n≤1000000), there may be the same elements in the array, please find out the kth (1≤k) ≤n) Small element, the k-th small element refers to the element at the k-th position sorted from small to large.
Input format:
Each input file is a test case. The first line of each file gives two positive integers n and k, and the second line gives n integers, separated by spaces.
Output format:
output the value of the k-th small element.
Input sample:
10 4
2 3 5 12 4 9 3 8 2 9
Output sample:
3
#include<bits/stdc++.h>
using namespace std;
vector<int> num;
int main(){
int n,k,x;
cin>>n>>k;
while(n--)cin>>x,num.push_back(x);
nth_element(num.begin(),num.begin()+k-1,num.end());
cout<<num[k-1];
}