Docsity

5 Questions on Algorithms and Data Structures - Assignment 2 | CS 200, Assignments of Computer Science

Toggle sidebar
bg1
CS200 Fall 2008 HW 2 due 9/30/08 at 3:00PM
1. Give a big-O estimate for each of these functions. For the function g in your estimate
f(x) is O(g), use a simple function g of smallest order.
a.
!
6x4+3x+12 +x!
b.
!
2+4x+8x2
c.
!
(x3+2x) /(2x+1)
d.
!
(3 +4x2)(log x+27)
e.
!
(2x+x2)(x4+3x)
2. In the manner of Examples 1 and 2 from section 3.2 of the Rosen text
a. show that
!
5x4+2x2+8
is
!
O(x5)
b. show that
!
2x2+x+1
is
!
O(x2)
3. Assuming you use k=1 as a witness, state whether the following functions are
!
O(x3)
,
and if so, state for what value of C.
a.
b.
!
10x+42
c.
!
2+4x+8x2
d.
!
(log x+1)(2x+3)
e.
!
2x+x!
4. What is the order of each of the following tasks in the worst case? in the average case?
in the best case? For each, state the conditions that make it worst, average or best case.
a. Printing all n integers in a sorted linked list.
b. Searching an array of strings for a particular one using linear search.
c. Reading one line at a time from a file, where each line is a set of tab-delimited
integers; putting each number in each set into an array, sorting it using
bubblesort and then printing the array.[count the operations that move data]
d. Adding an item to a stack that is implemented using an ArrayList.
5. Given the following algorithm, what is its Big-O? Justify your answer using the three
theorems presented in the Complexity notes (and found in section 3.2 of Rosen).
Assume the line “d” takes O(1) or O(c).
a. for (int pass = 1; pass <= n; ++pass) {
b. for (int index = 0; index < n; ++index) {
c. for (int count = 1; count < 10; ++count) {
d. sum = sum + aArray[pass] + bArray[index] + cArray[count];
e. }
f. }
g. }

Partial preview of the text

Download 5 Questions on Algorithms and Data Structures - Assignment 2 | CS 200 and more Assignments Computer Science in PDF only on Docsity!

CS200 Fall 2008 HW 2 due 9/30/08 at 3:00PM

  1. Give a big f(x) is O(g) - , use a simple functionO estimate for each of these functions. For the function g of smallest order. g in your estimate a. !

b.^6 x^4 +^3 x^ +^12 +^ x! !

c.^2 +^4 x^ +^8 x^2

!

d.^ ( x^3 +^2 x )^ /(^2 x^ +^1 )

!

e.^ (^3 +^4 x^2 )(log^ x^ +^ 27)

!

  1. In the manner of Examples 1 and 2 from section 3.2 of the Rosen text^ (^2 x^ +^ x^2 )( x^4 +^3 x^ ) a. show that !

5 x^4 + 2 x^2 + 8 is !

b. show that^ O ( x^5 ) !

2 x^2 + x + 1 is !

  1. Assuming you use k=1 as a witness, state whether the following functions are^ O ( x^2 ) !

and if so, state for what value of C.^^ O ( x^3 ), a. !

b.^3 x !

c.^10 x^ +^42 !

d.^2 +^4 x^ +^8 x^2

!

e.^ (log^ x^ +^1 )(2 x^ +^3 ) !

  1. What is the order of each of the following tasks in the worst case? in the average case?^2 x^ +^ x! in the best case? For each, state the conditions that make it worst, average or best case. a. Printing all n integers in a sorted linked list. b. c. Searching an arraReading one line at a time from a file, where each line is a set of taby of strings for a particular one using linear search.-delimited integers; putting each number in each set into an array, sorting it using bubblesort and then printing the array.[count the operations that move data]
  2. Given the following algorithm, what is its Big^ d.^ Adding an item to a stack that is implemented using an ArrayList.-O? Justify your answer using the three theorems presented in the Complexity notes (and found in section 3.2 Assume the line “d” takes O(1) or O(c). of Rosen). a. b. for (int pass = 1; pass <= n; ++pass) {for (int index = 0; index < n; ++index) { c. d. for (int count = 1; count < 10; ++count) {sum = sum + aArray[pass] + bArray[index] + cArray[count]; e. f. }} g. }