Finding Nth item of unsorted list without sorting the list

前端 未结 9 2089
耶瑟儿~
耶瑟儿~ 2020-12-09 03:41

Hey. I have a very large array and I want to find the Nth largest value. Trivially I can sort the array and then take the Nth element but I\'m only interested in one element

9条回答
  •  余生分开走
    2020-12-09 04:28

    You can iterate the entire sequence maintaining a list of the 5 largest values you find (this will be O(n)). That being said I think it would just be simpler to sort the list.

提交回复
热议问题