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

允我心安 提交于 2019-11-26 13:51:08

问题


Isnt Insertion sort O(n^2) > Quick sort O(nlogn)...so for a small n, wont the relation be the same?


回答1:


Big-O Notation describes the limiting behavior when n is large, also known as asymptotic behavior. This is an approximation. (See http://en.wikipedia.org/wiki/Big_O_notation)

Insertion sort is faster for small n because Quick Sort has extra overhead from the recursive function calls. Insertion sort is also more stable than Quick sort and requires less memory.

This question describes some further benefits of insertion sort. ( Is there ever a good reason to use Insertion Sort? )




回答2:


Define "small".

When benchmarking sorting algorithms, I found out that switching from quicksort to insertion sort - despite what everybody was saying - actually hurts performance (recursive quicksort in C) for arrays larger than 4 elements. And those arrays can be sorted with a size-dependent optimal sorting algorithm.

That being said, always keep in mind that O(n...) only is the number of comparisons (in this specific case), not the speed of the algorithm. The speed depends on the implementation, e. g., if your quicksort function as or not recursive and how quickly function calls are dealt with.

Last but not least, big oh notation is only an upper bound.

If algorithm A requires 10000 n log n comparions and algorithm B requires 10 n ^ 2, the first is O(n log n) and the second is O(n ^ 2). Nevertheless, the second will (probably) be faster.




回答3:


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.




回答4:


Its a matter of the constants that are attached to the running time that we ignore in the big-oh notation(because we are concerned with order of growth). For insertion sort, the running time is O(n^2) i.e. T(n)<=c(n^2) whereas for Quicksort it is T(n)<=k(nlgn). As c is quite small, for small n, the running time of insertion sort is less then that of Quicksort.....

Hope it helps...




回答5:


How about binary insertion sort? You can absolutely search the position to swap by using binary search.




回答6:


Good real-world example when insertion sort can be used in conjunction with quicksort is the implementation of qsort function from glibc.

The first thing to point is qsort implements quicksort algorithm with a stack because it consumes less memory, stack implemented through macros directives.

Summary of current implementation from the source code (you'll find a lot of useful information through comments if you take a look at it):

  1. Non-recursive

  2. Chose the pivot element using a median-of-three decision tree

  3. Only quicksorts TOTAL_ELEMS / MAX_THRESH partitions, leaving insertion sort to order the MAX_THRESH items within each partition. This is a big win, since insertion sort is faster for small, mostly sorted array segments.

  4. The larger of the two sub-partitions is always pushed onto the stack first

What is MAX_THRESH value stands for? Well, just a small constant magic value which

was chosen to work best on a Sun 4/260.



来源:https://stackoverflow.com/questions/8101546/why-is-insertion-sort-better-than-quick-sort-for-small-list-of-elements

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!