Call S A k 1 Sort the first two thirds It turns out that this algorithm will

Call s a k 1 sort the first two thirds it turns out

This preview shows page 2 - 4 out of 4 pages.

CallS(A[0..k-1]). (“Sort the first two-thirds.”)It turns out that this algorithm will correctly sort the input array. Let’s analyze its running time.(a) LetT(n) =the number of comparisons between array elements when executingSon an array of sizen. Write a recurrence relation forT(n).(b) Solve the recurrence relation you wrote down in part (a). Express your solution usingΘ(·)notation.Hint: You should be able to write your answer in the formT(n) =Θ(nc), for some constantc.(c) Based on your answer to part (b), would you expectSto be faster than, slower than, or about the samespeed as insertion sort?4. (20 pts.)Merge asymptoticsAfter grading the exams, the CS 170 staff want to sort all of the exams according to the student ID numbers,so that they can easily retrieve one if the need arises. There arenexams andkGSIs. Each GSI gathersn/kCS 170, Fall 2014, HW 2 2
Image of page 2
exams and sorts them into a pile. But then the GSIs leave and now David wants to merge all of these pilestogether. Merging two piles, one withaexams and the other withbexams, takesΘ(a+b)work. David hastwo options:1. Merge pile 1 with pile 2, then merge the result with pile 3, and then merge the result with pile 4, andso on.2. Split the piles into two roughly equal halves, recursively merge each half, and then use the mergeprocedure to combine the two halves.(a) How much work does David do if he uses method 1? Write a recurrence relation, then solve it. Expressyour final answer usingΘ(·)notation.(b) How much work does David do if he uses method 2? Write a recurrence relation, then solve it. Expressyour final answer usingΘ(·)notation.(c) Which method is better?5. (19 pts.)Plurality finding: divide-and-conquerDefinition.An arrayA[0..n-1]is said to have a 1/3-plurality element if some valuevappears>n/3 timesin the array; each such valuevis called a 1/3-plurality element.Design a divide-and-conquer algorithm that, givenA[0..n-1], outputs “Yes” and a 1/3-plurality elementifA[0..n-1]has a 1/3-plurality element, or “No” ifA[0..n-1]does not have a 1/3-plurality element. (IfA[0..n-1]has multiple 1/3-plurality elements, the algorithm can return any one of them.) Your algorithm
Image of page 3
Image of page 4

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture