This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: CS 234 Fall 2010 Assignment 4 Solutions 1. The following questions refer to num_silver in the posted solution to Question 2 of As signment 1. Briefly justify all your answers. Express runtimes in terms of n where n is the number of scores. (a) What is the worstcase runtime of num_silver ignoring scores . sort () ? [2 marks] reverse is Θ( n ) and each other operation is Θ(1) . Therefore the runtime ignoring both sort and reverse is determined by the number of iterations over both loops. In the worstcase, (no bronze medals awarded), these loops iterate over all n scores. Therefore the overall runtime ignoring reverse is Θ( n ) and including reverse still leaves a runtime of Θ( n ) . (b) If sort is implemented using mergesort, what is the worstcase runtime of num_silver ? [1 mark] The worstcase runtime of mergesort is Θ( n log n ) and so the runtime would be Θ( n + n log n ) or Θ( n log n ) . (c) If sort is implemented using quicksort, what is the worstcase runtime of num_silver ? [1 mark] The worstcase runtime of quicksort is Θ( n 2 ) and so the runtime would be Θ( n + n 2 ) or Θ( n 2 ) . (d) Describe how to implement sort so that the worstcase runtime of num_silver is O ( n ) . Be careful – remember that duplicate scores are possible. [4 marks] A modified version of bucketsort that handles duplicate scores can be used. Initialize each bucket to 0 instead of None . Then, instead of putting keys (scores) into buckets, increment the buckets. When removing keys (scores) from buckets, include as many copies of the score as the number in the bucket. Since only 800 buckets are needed, the runtime is O ( n + 800) or O ( n ) . Note: this algorithm is sometimes called countingsort. 1 2. Mergesort (as discussed in class and described in pseudocode below) is performed on an already sorted list of 2 k items for some positive integer k . Which item is involved in the most comparisons? Briefly justify your answer. [3 marks] Mergesort: if L has more than one item mergesort the first half of L mergesort the second half of L merge the two halves of L Merge: initialize S to be empty while M and N are both not empty if front of M < front of N...
View
Full Document
 Fall '09
 BARANOSKI
 Natural number, worstcase runtime

Click to edit the document details