This preview shows pages 1–3. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: Comp201: Discrete Computational Structures Fall Term, 2010 Homework 2: Solutions 1 Answers for Section 3.1 3.1.28 This could be thought of as just doing two iterations of binary search at once. We compare the soughtafter element to the middle element in the stillactive portion of the list, and then to the middle element of either the top half or the bottom half. This will restrict the subsequent search to one of four sublists, each about onequarter the size of the previous list. We need to stop when the list has length three or less and make explicit checks. Here is the pseudocode: procedure tetrasearch (x : integer, a 1 , a 2 , ..., a n : increasing integers) i := 1 j := n while i > j − 2 begin h := ( i + j ) / 4 m := ( i + j ) / 2 u := 3( i + j ) / 4 if x > a m then if x ≤ a u then begin i := m + 1 j := u end else i := u + 1 else if x > a h then begin i := h + 1 j := m end else j := h end if x = a i then location := i else if x = a j then 1 2 ANSWERS FOR SECTION 3.2 2 location := j else if x = a ( i + j ) / 2 then location := ( i + j ) / 2 else location := 0 Note that, location is the subscript of the term equal to x (0 if not found). 2 Answers for Section 3.2 3.2.4 We should find a pair ( C, k ) such that the following inequality holds:  2 x + 17  ≤ C ·  3 x  whenever x ≥ k . Note that 2 x + 17 and 3 x are always positive so that absolute signs can be cancelled. If x ≥ 5, then we have 2 x + 17 ≤ 2 x + 2 x = 2 · 2 x Obviously, 2 · 2 x ≤ 2 · 3 x for x ≥ 5, and therefore we have: 2 x + 17 ≤ 2 · 3 x implying 2 x + 17 is O (3 x ) with C = 2 and k = 5....
View Full
Document
 Spring '10
 RaulTekin
 Binary Search

Click to edit the document details