ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [Introduction to Algorithms, CLRS] Problems 2.1
    알고리즘 2018. 4. 20. 18:47

    This is a solution for Introduction to Algorithms. I write this for my study purpose.


    Problems 2-1 Insertion sort on small arrays in merge sort

    Although merge sort runs in Θ(n lg n) worst-case time and insertion sort runs in Θ(n^2) worst-case time, the constant factors in insertion sort can make it faster in practice for small problem sizes on many machines. Thus, it makes sense to coarsen the leaves of the recursion by using 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 insertion sort can sort the n/k sublists, each of length k, in Θ(nk) worst-case time.

    Suppose we have an array of length k, then it takes Θ(k^2) time to insertion-sort it. In this case, since we have n/k arrays to insertion-sort. It takes Θ(n/k  k^2) time to insertion sort all the arrays, which results in Θ(nk).


    b. Show how to merge the sublists in Θ(n lg (n/k)) worst-case time.

    Merging n/k sublists into n/2k sublists takes Θ(n) worst-case time.

    Merging n/2k sublists into n/4k sublists takes Θ(n) worst-case time.

    ...

    Merging 2 sublists into one list takes Θ(n) worst-case time.

    Since we have lg(n/k) such merges, merging n/k sublists into one list takes Θ(n lg(n/k)) worst-case time. 


    c. Given that the modified algorithm runs in Θ(nk + n lg (n/k)) worst-case time, what is the largest value of k as a function of n for which the modified algorithm has the same running time as standard merge sort, in terms of Θ-notation?

    In order for Θ(nk + n lg(n/k)) = Θ(n lg n), either nk = Θ(n lg n) or n lg(n/k) = Θ(n lg n). 

    From the above two possibilities, we know the largest asymptotic value for k is Θ(lg n). 


    d. How should we choose k in practice?

    In practice, k should be the largest list length on which insertion sort is faster than merge sort.

    To determine a practical value for k, we need to calculate the exact running time expressions with proper values for the constant factors.


    cf)

    http://cs.boisestate.edu/~jhyeh/teach/cs242_spring06/h1_sol.pdf

    http://ranger.uta.edu/~huang/teaching/CSE5311/HW1_Solution.pdf


    댓글

Designed by Tistory.