Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is insertion sort faster than quick-sort and bubble-sort for small cases?

I recently read an article that talked about the computation complexity of algorithms. The author mentioned "why insertion sort is faster than quick-sort and bubble-sort for small cases". Could anybody make some explanation for that?

Does anybody know the actual complexity of each sort algorithm I mentioned above?

like image 720
antonio081014 Avatar asked Oct 04 '11 04:10

antonio081014


1 Answers

Consider two complexity functions:

F(X) = X^2
G(X) = 4 * X * ln(X)

F(3) = 9
G(3) = 13

So algorithm F wins for 3 items. But:

F(100) = 10,000
G(100) = 1,842

So algorithm G wins for 100 items.

Insertion sort's complexity is like F(X). Quick sort's complexity is like G(X).

like image 148
David Schwartz Avatar answered Sep 21 '22 16:09

David Schwartz