algorithm - What is the worst-case time for insertion sort within merge sort? -
recently stumbled upon problem introduction algorithms edition 3
problem 2-1:
although merge sort runs in o(n logn)
worst-case time , insertion sort runs in o(n^2)
, latter runs faster small problem sizes. consider modification merge sort in n/k sublists of length k sorted using insertion sort , merged using standard merging mechanism.
(a) show insertion sort can sort n/k sublists, each of length k, in o(nk)
worst-case time.
the answer given is:
ans: insertion sort takes (k^2) time per k-element list in worst case. therefore, sorting n/k lists of k elements each takes (k^2 n/k) = (nk) worst-case time
how (k^2 n/k) given data?? im not understanding @ , greatlly appreciate explanation.
the sublists of length k, therefore insertion sort takes k^2
each sublist. now, there n/k
sublists in total, so, n/k
* k^2
nk
. key understanding here there n/k
number of sublists, , insertion sort takes k^2
time sort each one.
another thing note, knowing merge sort has o(n logn)
not important @ problem, because don't ask time sorting whole list, time sorting of sublists.
Comments
Post a Comment