c Lets develop a runtime recursion for an algorithm that computes f i

C lets develop a runtime recursion for an algorithm

This preview shows page 8 - 9 out of 9 pages.

c) Let’s develop a runtime recursion for an algorithm that computesfi(3by3blocks 7. (1 level)Optional bonus problem: More medians(This is anoptionalchallenge problem. It is not the most effective way to raise your grade in the course.Only solve it if you want an extra challenge.)We saw in class a randomized algorithm for computing the median, where the expected running time wasO(n). Design a algorithm for computing the median where theworst-caserunning time isO(n).Hint: It’s all in finding a good pivot. Pseudocode : CS 170, Fall 2016, HW 2 8
Image of page 8
1: function S ELECT ( k , A ) 2: n size of A 3: if n 100 then 4: L sorted version of A 5: return L [ n / 2 ] 6: M empty list of size n / 5 7: for i = 0 , 1 ,..., n / 5 - 1 do 8: M [ i ] median of A [ 5 i , 5 i + 4 ] 9: m Select ( n / 10 , M ) 10: A low elements of A less than m 11: A high elements of A greater than m 12: r rank of m in A (compute in linear time) 13: if k r then 14: return Select ( k , A low ) 15: else 16: return Select ( k - r , A high ) Running time analysis : The algorithm does linear work to recur on a list of size n / 5 and 3 n / 4. This yields a recursion of T ( n ) T ( 3 n / 4 )+ T ( n / 5 )+ n Writing out the recursion tree yields a bound of T ( n ) n +( 3 / 4 + 1 / 5 ) n +( 3 / 4 + 1 / 5 ) 2 n + ... = O ( n ) as desired.
Image of page 9

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture