Worse is better. Is there an example?

前端 未结 24 930
予麋鹿
予麋鹿 2020-12-12 19:03

Is there a widely-used algorithm that has time complexity worse than that of another known algorithm but it is a better choice in all practical si

24条回答
  •  眼角桃花
    2020-12-12 19:34

    Mergesort versus Quicksort

    Quick sort has an average time complexity of O(n log n). It can sort arrays in place, i.e. a space complexity of O(1).

    Merge sort also has an average time complexity of O(n log n), however its space complexity is much worse: Θ(n). (there is a special case for linked lists)

    Because of the worst case time complexity of quick sort is Θ(n^2) (i.e. all elements fall on the same side of every pivot), and mergesort's worst case is O(n log n), mergesort is the default choice for library implementers.

    In this case, I think that the predictability of the mergesort's worst case time complexity trumps quicksorts much lower memory requirements.

    Given that it is possible to vastly reduce the likelihood of the worst case of quicksort's time complexity (via random selection of the pivot for example), I think one could argue that mergesort is worse in all but the pathological case of quicksort.

提交回复
热议问题