Popular Posts

Sunday, August 7, 2011

CLRS - Problem 2.1

Problem:

Although merge sort runs in Θ(n lg n) worst-case time and insertion sort runs in Θ(n2) worstcase
time, the constant factors in insertion sort make it faster for small n. Thus, it makes sense
to use insertion sort within merge sort when subproblems become sufficiently small. Consider
a modification to merge sort in which n/k sublists of length k are sorted using insertion sort
and then merged using the standard merging mechanism, where k is a value to be determined.
a. Show that the n/k sublists, each of length k, can be sorted by insertion sort in Θ(nk)
worst-case time.
                  Time to sort each list of length k using insertion sort= T(k^2)
                  Time to sort n/k lists each of length k = T(n/k *  k^2) = Θ(nk)
                                                           
b. Show that the sublists can be merged in Θ(n lg (n/k) worst-case time.
                  Height of the tree with n elements and with k = log n - log k = log(n/k)
                  so for merging it will take Θ(n * log(n/k))
c. Given that the modified algorithm runs in Θ(nk + n lg (n/k)) worst-case time, what is
the largest asymptotic (Θnotation) value of k as a function of n for which the modified
algorithm has the same asymptotic running time as standard merge sort?
Let k=n,
it will become Θ(n^2 + n lg(1)) = Θ(n^2)

Lets take k=1,
it will become Θ(n + n lg n)

Lets take k= lg n
it will become Θ(n lg n + n lg(n/(lg n)) => Θ(n lg n + n lg n - n lg(lg n)) => Θ(2 lg n - n lg(lg n)) => Θ(n lg n)

So, k= Θ(lg n)
d. How should k be chosen in practice?



In practise k should be taken greater the lg n depending upon the constant factors.



No comments:

Post a Comment