{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Discrete Mathematics with Graph Theory (3rd Edition) 232

# Discrete Mathematics with Graph Theory (3rd Edition) 232 -...

This preview shows page 1. Sign up to view the full content.

230 Solutions to Exercises Note that we check both possibilities n = 1 and n = 2 so that there is no possibility that f. = 0 in what follows. (b) To detennine the number of comparisons required by the algorithm, first suppose that n = 3 k is a power of 3. The algorithm proceeds by progressively dividing the list into thirds, so it terminates after the while statement-n > O?-has been encountered k times. As long as n > 2, three comparisons are needed to check n > O? n = I? n = 2? and a further two comparisons are needed to determine if x :::; ae and x > am, giving at most 5k comparisons in all. Finally, a list with one or two elements will require at most an additional four comparisons. Thus, for a list of length 3 k , our algorithm requires 5k + 4 comparisons. If n is not a power of 3, find k such that 3 k - 1 < n :::; 3 k and increase the length of the input list, if necessary, by adding terms all equal to an until the extended list has length 3 k . This list, and hence the original, is searched after
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Ask a homework question - tutors are online