Homework 1 Answer Key | Algorithms and Data Structures | CS 200, Assignments of Computer Science

Material Type: Assignment; Class: Algorithms and Data Structures; Subject: Computer Science; University: Colorado State University; Term: Unknown 1989;

Typology: Assignments

Pre 2010

Uploaded on 11/08/2009

koofers-user-7is
koofers-user-7is 🇺🇸

5

(1)

10 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
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)!=!
x
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.!
pf2

Partial preview of the text

Download Homework 1 Answer Key | Algorithms and Data Structures | CS 200 and more Assignments Computer Science in PDF only on Docsity!

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) = x^2 +x^3 log(x) d=4, not 3 because log(x) gets larger than any constant (b) f(x) = x^6 +1000x^4 + d= (c) f(x) = € x 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>= 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 >= 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? 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.
  1. (a) Show that € i = O ( n^2 i = 1 n

_The sum of 1 to n is n(n+1)/2 so O(n_^2 ) (b) Write a O(1) method that computes the above sum. _public int sum1to(intn) { return n(n+1)/2; }_

  1. Explain why the complexity of producing all bitstrings of length n is O(n 2n). There are 2n^ different bitstrings of length n. To produce n bits takes O(n). Hence the complexity of producing them all is O(n.2n)