This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: Solutions to Homework Four CS 191 : Discrete Structures I Mar. 6 , Winter 2007 Due Date: Mar. 20, 2007, Tuesday, before the class meeting. Section 4.2, p155 Exercise (15) Write an algorithm whose input is a sequence s 1 , …, s n sorted in nondecreasing order and a value x . (Assume that all the values are real numbers.) The algorithm inserts x into the sequence so that the resulting sequence is sorted in nondecreqsing order. Example: If the sequence is 2, 6, 12, 14, and x = 5, the resulting sequence is 2, 5, 6, 12, 14. Answer : insert ( s , n , x ){ i :=1 while ( i ≤ n ) and x > s i do i := i + 1 // x should be inserted in front of the i th location. We need to right shift s i , …, s n one place. j := n while ( j ≥ i ) { do { s i +1 := s i j := j – 1 } //Insert x s i := x } Exercise (18) Describe the worst case input for algorithm 4.2.1. Answer : The worst case occurs when the for loop and the while loop run as long as possible. This situation is achieved when t consists of n 0’s and p consists of m1 0’s followed by one 1. 1 Section 4.3, p168 Select a theta notation from Table 4.3.3 for each expression in exercises 112. Exercise (6) 4 6 6 + + n n Answer: 4 6 6 + + n n = Θ ( n 6 ) Exercise (9) (6 n + 4)(1+lg n ) Answer : (6 n + 4)(1+lg n ) = Θ ( n lg n ) Exercise (11) n n n n n 2 2 ) 1 )( lg ( + + + Answer : n n n n n 2 2 ) 1 )( lg ( + + + = Θ ( n ) Exercise (12) 2+4+8+16+…+ 2 n Answer: 2+4+8+16+…+ 2 n = 2 n +1 – 2 = Θ (2 n ) In Exercises 1315, select a theta notation for f(n) + g(n). 2 Exercise (15) f ( n ) = Θ ( n 3/2 ), g ( n ) = Θ ( n 5/2 ), Answer : f(n) + g(n) = Θ ( n 3/2 ) + Θ ( n 5/2 ) = Θ ( n 5/2 ), In exercises 1626, select a theta notation from among Θ (1), Θ (lg n ), Θ ( n ), Θ ( n lg n ), Θ ( n 2 ), Θ ( n 3 ),...
View
Full
Document
This note was uploaded on 04/12/2008 for the course CS 191 taught by Professor Shen during the Winter '06 term at University of MissouriKansas City .
 Winter '06
 Shen
 Sort

Click to edit the document details