How many elements would have to be looked to locate these elements?

343 Views Asked by At

How many elements would have to be looked (binary search) at in order to find an integer 5000 at element 499 and an integer 7282 at element 686 assuming it's binary search algorithm and the total number of elements in the array are 1000? I do know that for the binary search algorithm you have divide the total number of elements that would be in your array(which is 1000) in half and keep doing until you find the desired element! Can someone help me? Please!

I do have a theory though (see my theory below:)

1000/2= the first element is 500 element

500/2 the next element is 250 element.

1

There are 1 best solutions below

17
On

here's a worked example of roughly how the algorithm works:

[10,20,30,40,50,60,70,80,90,100]

I want to find 70:

I take the rough middle element ( which is 50) I compare it to 70 and:

if 50<70 I take roughly the upper half of the values if 50>70 I take roughly the lower half of the values if 50=70 I am finished

since 50<70 I take roughly the upper half of the values: [60,70,80,90,100] I then take the middle element (80) and compare that to 70 since 80>70 I take roughly the lower half of the values I took before:

[60,70,80] I take the rough middle element ( 70) and compare it to 70 since 70=70 I am now finished.