CSCI 4041: Algorithms and Data Structures
(Fall’09)
Homework 1, Due 09/24/09
Answer all of the following questions, and always explain your answer. Good Luck!
1. (50 points) Consider the following sorted search problem:
Input: A sequence of
n
distinct sorted numbers
A
=
h
a
1
, a
2
, . . . , a
n
i
and a number
v
.
Output: An index
i
such that
A
[
i
]
≤
v < A
[
i
+ 1]
. If
v < a
1
, then output 0. If
v
≥
a
n
,
then output
n
.
(a) (15 points) Give an algorithm (pseudocode) for solving the problem by scanning the entire
sequence. Using a loop invariant, show that your algorithm is correct.
(b) (15 points) Instead of scanning the entire sequence, since
A
is sorted, we can check the midpoint
of the sequence against
v
and eliminate half of the sequence from further consideration.
Binary
search
is an algorithm that repeats this procedure, halving the size of the remaining portion of
the sequence each time. Write pseudocode, either iterative or recursive, for binary search. Argue
that the worstcase running time of binary search is
This is the end of the preview.
Sign up
to
access the rest of the document.
 Spring '08
 karypis
 Algorithms, Binary Search, Data Structures, Sort, Insertion Sort, Big O notation

Click to edit the document details