Is there ever a good reason to use Insertion Sort?

前端 未结 7 2145
再見小時候
再見小時候 2020-11-27 14:48

For general-purpose sorting, the answer appears to be no, as quick sort, merge sort and heap sort tend to perform better in the average- and worst-case scenarios. However, i

7条回答
  •  生来不讨喜
    2020-11-27 15:12

    From http://www.sorting-algorithms.com/insertion-sort:

    Although it is one of the elementary sorting algorithms with O(n2) worst-case time, insertion sort is the algorithm of choice either when the data is nearly sorted (because it is adaptive) or when the problem size is small (because it has low overhead).

    For these reasons, and because it is also stable, insertion sort is often used as the recursive base case (when the problem size is small) for higher overhead divide-and-conquer sorting algorithms, such as merge sort or quick sort.

提交回复
热议问题