Docsity

CS200 Fall 09 Homework: Big O Notation Analysis, Assignments of Computer Science

Toggle sidebar
bg1
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)!=!
€
x
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!
€
i = O(n2
i=1
n
∑)
!
!!!!!(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)!.!

Partial preview of the text

Download CS200 Fall 09 Homework: Big O Notation Analysis and more Assignments Computer Science in PDF only on Docsity!

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) = x^2 +x^3 log(x) (b) f(x) = x^6 +1000x^4 + (c) f(x) = € x 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>= 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 >= if (x==0) return 0; else if (x%2==1) return rmul(x/2,y2)+y; else return rmul(x/2,y2); } (a) What does rmul compute? (b) What is the lowest big‐O bound for its run time complexity?
  5. (a) Show that € i = O ( n^2 i = 1 n

(b) Write a O(1) method that computes the above sum.

  1. Explain why the complexity of producing all bitstrings of length n is O(n 2n).