Search code examples
algorithmtime-complexityasymptotic-complexityspace-complexitycode-complexity

Big O(n logn) is not preferable over the O(n^2)


Any Algorithms example when do we prefer Big O(n^2) time complexity over the O(n logn)? I have seen this question somewhere but did not find answer.


Solution

  • There are several reasons to choose an algorithm with a higher time complexity:

    • Speed: The asymptotic complexity only applies to values of n greater than some n_0. Also, it assumes a certain machine underneath which only partially matches real machines with multiple levels of cache and constrained memory.
    • Space: Some algorithms require more space than others, and thus become impossible to implement. Also, this may simply influence the speed on a real machine. For example, locality of references has influence on cache hits or misses, which is the reason why Quicksort performs better than Mergesort.
    • Implementation complexity: In some cases the loss in performance is simply negligible, but the development time isn't.