Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 933
user1031752 Avatar asked Nov 12 '11 00:11

user1031752


People also ask

Which is better insertion sort or quick sort?

Quicksort algorithm is efficient if the size of the input is very large. But, insertion sort is more efficient than quick sort in case of small arrays as the number of comparisons and swaps are less compared to quicksort. So we combine the two algorithms to sort efficiently using both approaches.

Is insertion sort best for small data sets?

Insertion Sort is a good choice for "small" data sets.

Is quicksort good for small data sets?

"Quick Sort works well on small arrays of data, but merge sort is better for large arrays" [duplicate]

Why is insertion sort better?

Insertion sort has a fast best-case running time and is a good sorting algorithm to use if the input list is already mostly sorted. For larger or more unordered lists, an algorithm with a faster worst and average-case running time, such as mergesort, would be a better choice.


1 Answers

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? )

like image 173
Casey Robinson Avatar answered Oct 07 '22 08:10

Casey Robinson