You misunderstand what nth_element
does, and are trying to use it incorrectly.
This function takes a range which is not necessarily sorted, and partially sorts it such that nth element is in its correct place. If you ise this function to find a median, you don't need to sorf first.
If you already have a sorted range [first, last)
then nth element of this range is pointed to by first + n
.