CS200%Fall09%Homework%1%
%%%%%%%%
%%%Key%
%
!
1.!!For!the!following!functions!find!the!least!integer!d!such!that!f(x)!is!O(xd).!
!
!!(a)!f(x)!=!x2+x3log(x)!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!d=4,%not%3%because%log(x)%gets%larger%than%any%constant%
!!(b)!f(x)!=!x6+1000x4+100000!
!!!!!!!!!!!!!!!!!d=6%%%
!!(c)!f(x)!=!
!
!!!!!!!!!!!!!!!!!%d=0,%not%:1/2%because%we%asked%for%the%smallest%integer%
!
2.!!Given!an!Arraylist!of!length!n.!What!is!the!run!time!complexity!of!adding!an!
element!in!the!middle!!of!the!Arraylist?!
%%%%%%%O(n)%because%n/2%elements%need%to%be%shifted%right,%or%the%whole%arraylist%
%%%%%%%(n%elements)%may%need%to%get%copied%into%a%larger%area%
!
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?!!!!!!x%times%y!
!(b)!!What!is!the!lowest!big‐O!bound!for!its!run!time!complexity?%%%%%%%%%%%%%O(x)!
!
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?!!!x%times%y!
(b)!What!is!the!lowest!big‐O!bound!for!its!run!time!complexity?!!!!!!O(log(x)),%
because%x%gets%halved%each%recursive%call.!