   Chapter 11.5, Problem 18ES ### Discrete Mathematics With Applicat...

5th Edition
EPP + 1 other
ISBN: 9781337694193

#### Solutions

Chapter
Section ### Discrete Mathematics With Applicat...

5th Edition
EPP + 1 other
ISBN: 9781337694193
Textbook Problem
1 views

# Suppose an array of length k is input to the while loop of the modified binary search algorithm. Show that after one iteration of the loop. if a [ m i d ] ≠ x , the input to the next iteration is an array of length at most ⌊ k / 2 ⌋ .

To determine

Show that after one iteration of the loop, if a[mid]x, the input to the next iteration is an array of length at most k/2.

Explanation

Given information:

Suppose an array of length k is input to the while loop of the modified binary search algorithm.

Proof:

Let the input to the while-loop of the modified binary search algorithm contain k elements, thus a[bot],a[bot+1],...,a[top] contains k elements.

By the number of elements in a list theorem, we also know that the number of integers from bot to top top is top − bot + 1 and thus a[bot],a[bot+1],...,a[top] contains top + bot − 1 element

top+bot1=k

We then assign mid to the upper of the two middle indices of the array.

mid=bot+top2

If a[mid]x, then either a[mid]<x or a[mid]>x.

FIRST CASE: a[mid]<x

If a[mid]<x, then we assign mid − 1 to top and thus the input of the next iteration is then a[bot],a[bot+1],...,a[mid1]. By the number of elements in a list theorem, we also know that the number of integers from bot to mid − 1 top is ( mid − 1) − bot + 1 = mid − bot and thus a[bot],a[bot+1],...,a[mid1] contains mid − bot elements.

Length array next iteration = mid − bot

=bot+top2bot

=k+12bot                                              top+bot1=k

k+121                                                            as bot1

=k+121

=k+122

=k12

k12=k2=k2=k2 when k even and thus k1 odd

k12=k12=k2 when k odd and thus k1 even

=k2

SECOND CASE: a[mid]>x

If a[mid]>x, then we assign mid + 1 to bot and thus the input of the next iteration is then a[mid+1],a[mid+2],

### Still sussing out bartleby?

Check out a sample textbook solution.

See a sample solution

#### The Solution to Your Study Problems

Bartleby provides explanations to thousands of textbook problems written by our experts, many with advanced degrees!

Get Started 