This will find the nth element in an unsorted array as if it was sorted, and I want it to run below n log n time, but I'm not entirely sure the run time of this, would it be O(n^2)?
int nthElement(std::vector<int> vec, int n)
{
n--;
int smallest = vec[0];
int index = 0;
for(int i = 0; i < vec.size(); i++)
{
if (vec[i] < smallest)
{
smallest = vec[i];
index = i;
}
}
vec.erase(vec.begin() + index);
if( n == 0)
{
std::cout << "Do I get here?" << std::endl;
return smallest;
}
return nthElement(vec, n);
}