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

前端 未结 6 685
不知归路
不知归路 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

    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...

提交回复
热议问题