

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


Due 2/12/09 before class
Always justify your answers. All algorithms should be as efficient as possible and all runtimes should be as tight as possible.
for(i=2; i<=n; i++){ if(RandomInteger(2)==1){ for(j=1; j<=n; j++){ print(‘‘hello’’); } }
if(RandomInteger(i)==i){ for(j=1; j<=n; j++){ for(k=1; k<=n; k++){ print(‘‘hello’’); } } } }
a) What is the best case runtime, in terms of n, of this code snippet? Describe what triggers a best-case scenario. b) What is the worst case runtime, in terms of n, of this code snippet? Describe what triggers a worst-case scenario. c) Now analyze the expected runtime. Clearly define your random variable. Hint: Break your random variable into multiple random variables.
Flip over to back page =⇒
852 , 117 , 505 , 143 , 156 , 856 , 806 , 145 , 555 , 588 , 112
using radix sort but starting with the most significant digit (so, from left to right, not from right to left). Why would a program that implements this strategy be much more complicated than the radix sort that starts with the least significant digit? (Hint: What kind of variables or data structures would you have to maintain?)
Consider (deterministic) quicksort which takes the first array-element as the pivot. Design an example input of 100 numbers which causes quicksort to always split 13 : 23 in each recursive call. (Round fractions as necessary.)
Given n numbers between 0 and nn. Which sorting algorithm is faster: Insertion Sort or Radix Sort? Justify your answer.