why is merge sort preferred over quick sort for sorting linked lists

前端 未结 3 1745
孤城傲影
孤城傲影 2020-12-22 18:57

I read the following in a forum :

Merge sort is very efficient for immutable datastructures like linked lists

and

3条回答
  •  没有蜡笔的小新
    2020-12-22 19:40

    A quicksort will move records in to the middle of the list. In order to move an item to index X, it has to start at 0 and iterate one record at a time.

    A mergesort splits the list into several small lists and only ever compares the items head of the lists.

    The setup for a merge sort is typically move expensive than the iterated required by a quicksort. However, when a list is sufficiently large, or the reads are expensive(like from a disk), the time it takes for the quicksort to iterate becomes a major factor.

提交回复
热议问题