Docsity

MA 522 Fall 2008 Homework 1: Linear and Binary Search Algorithms - Prof. Agnes Szanto, Assignments of Mathematics

Toggle sidebar
bg1
Homework 1
MA 522 Fall 2008
1. Consider the searching problem:
Input A sequence of nnumbers A= [a1, . . . , an]and a value v.
Output An index isuch that v=A[i]or the special value NI L if vdoes not appear in A
(a) Write a pseudocode for LINEAR SEARCH, which scans through the sequence, look-
ing for v. Using a loop invariant, prove that your algorithm is correct. (Make sure that
your loop invariant fulfills the three necessary properties – initialization, maintenance,
termination.)
(b) How many elements of the input sequence need to be checked on the avarage,
assuming that the element being searched for is equally likely to be any element in the
array? How about the worst case? What are the avarage-case and worst-case running
times of linear search in Θnotation? Justify your answer.
(c) If we assume that Ais sorted, then we can check the midpoint of the sequence
against vand 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 a pseudocode, either iterative or recursive, for binary
search.
(d) Use the master method to show that the solution to the binary-search recurrence
T(n) = T(n/2) + Θ(1) is T(n) = Θ(lg n), and argue that the worst case running time for
binary search is Θ(lg n).
(e) Observe that the while loop of the INSERTION SORT procedure (in class) uses a
linear search to scan (backward) through the sorted subarray A[1 . . . j −1]. Can we use
binary search instead to improve the overall worst-case running time of insertion sort to
Θ(nlg n)?
2.
Let f, g :N→Rbe functions which are positive for some n≥n0. Prove the equatlity
O((f+g)2) = O(f2+g2). (Hint: you need to use the ineaquality between geometric and
arithmetic averages.)
3.
Give asymptotic upper and lower bounds for the recurrence
T(n) = T(n/2) + T(n/4) + T(n/8) + n.
1

Partial preview of the text

Download MA 522 Fall 2008 Homework 1: Linear and Binary Search Algorithms - Prof. Agnes Szanto and more Assignments Mathematics in PDF only on Docsity!

Homework 1

MA 522 Fall 2008

  1. Consider the searching problem :

Input A sequence of n numbers A = [a 1 ,... , an] and a value v.

Output An index i such that v = A[i] or the special value NIL if v does not appear in A

(a) Write a pseudocode for LINEAR SEARCH, which scans through the sequence, look- ing for v. Using a loop invariant, prove that your algorithm is correct. (Make sure that your loop invariant fulfills the three necessary properties – initialization, maintenance, termination.)

(b) How many elements of the input sequence need to be checked on the avarage, assuming that the element being searched for is equally likely to be any element in the array? How about the worst case? What are the avarage-case and worst-case running times of linear search in Θ notation? Justify your answer.

(c) If we assume that A is sorted, then 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 a pseudocode, either iterative or recursive, for binary search.

(d) Use the master method to show that the solution to the binary-search recurrence T (n) = T (n/2) + Θ(1) is T (n) = Θ(lg n), and argue that the worst case running time for binary search is Θ(lg n).

(e) Observe that the while loop of the INSERTION SORT procedure (in class) uses a linear search to scan (backward) through the sorted subarray A[1... j − 1]. Can we use binary search instead to improve the overall worst-case running time of insertion sort to Θ(n lg n)?

Let f, g : N → R be functions which are positive for some n ≥ n 0. Prove the equatlity O((f + g)^2 ) = O(f 2 + g^2 ). (Hint: you need to use the ineaquality between geometric and arithmetic averages.)

Give asymptotic upper and lower bounds for the recurrence

T (n) = T (n/2) + T (n/4) + T (n/8) + n.