Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Consider the following pseudocode for finding an element in a sorted array A1.n 1: BINARYSEARCH(A[1..n], x) 2: left = 1 3: while (right 2 left)

image text in transcribed

Consider the following pseudocode for finding an element in a sorted array A1.n 1: BINARYSEARCH(A[1..n], x) 2: left = 1 3: while (right 2 left) mid = left + right-left 5: 6: 7 8: 9: 10 return mid else if a Amid left- mid +1 12: return NOTFOUND (a) State precisely the loop invariant for the while loop in lines 4-11 and prove that this loop invariant holds. Your proof should use the structure of the loop invariant proof presented in Chapter 2 of CLRS Conclude that if r is present in the sorted array A, correctly returns the index of r. (b) Prove by induction that the while loop in lines 4-11 l execute 1 log n times in the worst case. (Hint observe what happens to the size of the subarray Alleft..right] after each iterution.) Conclude that the running time of the BINARYSEARCH algorithm is (log n)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

The Database Experts Guide To Database 2

Authors: Bruce L. Larson

1st Edition

0070232679, 978-0070232679

More Books

Students also viewed these Databases questions

Question

b. Where did they come from?

Answered: 1 week ago