Why does Haskell use mergesort instead of quicksort?

前端 未结 6 834
执念已碎
执念已碎 2021-01-31 01:38

In Wikibooks\' Haskell, there is the following claim:

Data.List offers a sort function for sorting lists. It does not use quicksort; rather, it uses an ef

6条回答
  •  自闭症患者
    2021-01-31 02:06

    I am not sure, but looking at the code i don't think Data.List.sort is Mergesort as we know it. It just makes a single pass starting with the sequences function in a beautiful triangular mutual recursive fashion with ascending and descending functions to result in a list of already ascending or descending ordered chunks in the required order. Only then it starts merging.

    It's a manifestation of poetry in coding. Unlike Quicksort, its worst case (total random input) has O(nlogn) time complexity, and best case (already sorted ascending or descending) is O(n).

    I don't think any other sorting algorithm can beat it.

提交回复
热议问题