Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

1. Given a sorted array A, and x, consider the problem of finding an index i of A, such that A[i]

1. Given a sorted array A, and x, consider the problem of finding an index i of A, such that A[i] <= x < A[i+1]. In other words, the function should return the index of the rightmost occurrence of x, if x is in A, and the index of floor(x), if x is not in A. Assume A has sentinels, A[0] = -infinity and A[n-1] = +infinity, where n = A.length. Prove that the following algorithm solves the problem correctly: binarySearch(A, p, r, x): n <-- r-p+1 if n <= 2 then return p else q <-- (p+r)/2 if x < A[q] then return binarySearch(A, p, q, x) else /* x >= A[q] */ return binarySearch(A, q, r, x) 2. Prove that the following algorithm is incorrect for the same problem: binarySearch(A, p, r, x): n <-- r-p+1 if n <= 2 then return p else q <-- (p+r)/2 if x < A [q] then return binarySearch(A, p, q-1, x) else if x = A [q] then return q else /* x > A[q] */ return binarySearch(A, q+1, r, x) 

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_2

Step: 3

blur-text-image_3

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

Database And Expert Systems Applications 23rd International Conference Dexa 2012 Vienna Austria September 2012 Proceedings Part 1 Lncs 7446

Authors: Stephen W. Liddle ,Klaus-Dieter Schewe ,A Min Tjoa ,Xiaofang Zhou

2012th Edition

3642325998, 978-3642325991

Students also viewed these Databases questions

Question

What advice would you provide to Jennifer?

Answered: 1 week ago

Question

What are the issues of concern for each of the affected parties?

Answered: 1 week ago