Finding smallest value in an array most efficiently

后端 未结 13 1677
慢半拍i
慢半拍i 2020-11-29 07:02

There are N values in the array, and one of them is the smallest value. How can I find the smallest value most efficiently?

13条回答
  •  自闭症患者
    2020-11-29 07:58

    If finding the minimum is a one time thing, just iterate through the list and find the minimum.

    If finding the minimum is a very common thing and you only need to operate on the minimum, use a Heap data structure.

    A heap will be faster than doing a sort on the list but the tradeoff is you can only find the minimum.

提交回复
热议问题