CS200%Fall09%Homework%1%
student:%
id:%
!
1.!!For!the!following!functions!find!the!least!integer!d!such!that!f(x)!is!O(xd).!
!
!!(a)!f(x)!=!x2+x3log(x)!
!!!
!!(b)!f(x)!=!x6+1000x4+100000!
!! !!!
!!(c)!f(x)!=!
!
!
2.!!Given!an!Arraylist!of!length!n.!What!is!the!run!time!complexity!of!adding!an!
element!in!the!middle!!of!the!Arraylist?!
!
3.!Given!the!following!java!method:!
!
!!!!!!!!int!lmul(int!x,!int!y)!{!
!!!!!!!!!!!!!//!pre:!x>=0!
!!!!!!!!!!!!!int!p=0;!
!!!!!!!!!!!!!while!(x>0){!
!!!!!!!!!!!!!!!!p+=y;!
!!!!!!!!!!!!!!!!x‐‐;!
!!!!!!!!!!!!!}!
!!!!!!!!!!return!p;!!!!
!!!!!!!!}!
!(a)!!What!does!lmul!compute?!
!(b)!!What!is!the!lowest!big‐O!bound!for!its!run!time!complexity?!
!
4.!Given!the!following!java!method!
!!!!!!!!int!rmul(int!x,!int!y){!
!!!!!!!!!!!!//!pre:!x!>=0!
!!!!!!!!!!!!if!(x==0)!return!0;!
!!!!!!!!!!!!else!if!(x%2==1)!return!rmul(x/2,y*2)+y;!
!!!!!!!!!!!!!!!!!!!!!else!return!rmul(x/2,y*2);!
!!!!!!!!}!
(a)!What!does!rmul!compute?!
(b)!What!is!the!lowest!big‐O!bound!for!its!run!time!complexity?!
5.!(a)!Show!that!
!
!!!!!(b)!Write!a!O(1)!method!that!computes!the!above!sum.!
!
6.!Explain!why!the!complexity!of!producing!all!bitstrings!of!length!n!is!O(n!2n)!.!