Are there any cases where you would prefer a higher big-O time complexity algorithm over the lower one?

后端 未结 22 1499
说谎
说谎 2020-11-28 01:06

Are there are any cases where you would prefer O(log n) time complexity to O(1) time complexity? Or O(n) to O(log n)?

22条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-28 01:26

    1. When the "1" work unit in O(1) is very high relative to the work unit in O(log n) and the expected set size is small-ish. For example, it's probably slower to compute Dictionary hash codes than iterate an array if there are only two or three items.

    or

    1. When the memory or other non-time resource requirements in the O(1) algorithm are exceptionally large relative to the O(log n) algorithm.

提交回复
热议问题