101k views
4 votes
Consider the following list.

list = {5, 11, 25, 28, 45, 78, 100, 120, 125};

Suppose that binary search as discussed in the book is used to determine whether 110 is in list. Exactly how many key comparisons are executed by binary search?

A) 3

B) 5

C) 8

D) 12

User Meggy
by
7.9k points

1 Answer

5 votes

Answer:

To search 110 is in list we key comparisons binary search will make are option

(A) 3

Step-by-step explanation:

First the start =0 and end =8 and midindex=4.

So 110 will be compared to element at index 4 that is 45 since 110 is greater than 45 so start will be updated as start=midindex +1.

start=5 , end =8 and midindex=6.

Element at index 6 is 100 so 110 will be compared to 100 since it is greater so again start will be updated midindex+1.

start=7,end=8 midindex=7

element at index 7 is 120 so 110 will be compared to 120 since it is smaller than 120 end will be updated as midindex -1.

start=7,end=6

Now we have start > end .So the loop will finish execution.

Comparison made are 3.

User Towry
by
7.9k points
Welcome to QAmmunity.org, where you can ask questions and receive answers from other members of our community.