O(log N) == O(1) - Why not?

后端 未结 23 2096
广开言路
广开言路 2020-12-22 18:17

Whenever I consider algorithms/data structures I tend to replace the log(N) parts by constants. Oh, I know log(N) diverges - but does it matter in real world applications?

23条回答
  •  佛祖请我去吃肉
    2020-12-22 18:49

    The observation that O(log n) is oftentimes indistinguishable from O(1) is a good one.

    As a familiar example, suppose we wanted to find a single element in a sorted array of one 1,000,000,000,000 elements:

    • with linear search, the search takes on average 500,000,000,000 steps
    • with binary search, the search takes on average 40 steps

    Suppose we added a single element to the array we are searching, and now we must search for another element:

    • with linear search, the search takes on average 500,000,000,001 steps (indistinguishable change)
    • with binary search, the search takes on average 40 steps (indistinguishable change)

    Suppose we doubled the number of elements in the array we are searching, and now we must search for another element:

    • with linear search, the search takes on average 1,000,000,000,000 steps (extraordinarily noticeable change)
    • with binary search, the search takes on average 41 steps (indistinguishable change)

    As we can see from this example, for all intents and purposes, an O(log n) algorithm like binary search is oftentimes indistinguishable from an O(1) algorithm like omniscience.

    The takeaway point is this: *we use O(log n) algorithms because they are often indistinguishable from constant time, and because they often perform phenomenally better than linear time algorithms.

    Obviously, these examples assume reasonable constants. Obviously, these are generic observations and do not apply to all cases. Obviously, these points apply at the asymptotic end of the curve, not the n=3 end.

    But this observation explains why, for example, we use such techniques as tuning a query to do an index seek rather than a table scan - because an index seek operates in nearly constant time no matter the size of the dataset, while a table scan is crushingly slow on sufficiently large datasets. Index seek is O(log n).

提交回复
热议问题