Why is Insertion sort better than Quick sort for small list of elements?

前端 未结 6 686
不知归路
不知归路 2020-12-01 07:47

Isn\'t Insertion sort O(n^2) > Quicksort O(n log n)...so for a small n, won\'t the relation be the same?

6条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-01 08:27

    O()-notation is typically used to characterize performance for large problems, while deliberately ignoring constant factors and additive offsets to performance.

    This is important because constant factors and overhead can vary greatly between processors and between implementations: the performance you get for a single-threaded Basic program on a 6502 machine will be very different from the same algorithm implemented as a C program running on an Intel i7-class processor. Note that implementation optimization is also a factor: attention to detail can often get you a major performance boost, even if all other factors are the same!

    However, the constant factor and overhead are still important. If your application ensures that N never gets very large, the asymptotic behavior of O(N^2) vs. O(N log N) doesn't come into play.

    Insertion sort is simple and, for small lists, it is generally faster than a comparably implemented quicksort or mergesort. That is why a practical sort implementation will generally fall back on something like insertion sort for the "base case", instead of recursing all the way down to single elements.

提交回复
热议问题