a4solutions

a4solutions - CS 234 Fall 2010 Assignment 4 Solutions 1....

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the 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 worst-case 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 worst-case, (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 worst-case runtime of num_silver ? [1 mark] The worst-case 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 worst-case runtime of num_silver ? [1 mark] The worst-case 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 worst-case runtime of num_silver is O ( n ) . Be careful remember that duplicate scores are possible. [4 marks] A modified version of bucket-sort 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 counting-sort. 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

This note was uploaded on 01/27/2011 for the course CS 234 taught by Professor Baranoski during the Fall '09 term at Waterloo.

Page1 / 5

a4solutions - CS 234 Fall 2010 Assignment 4 Solutions 1....

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online