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

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.

• where are you having the trouble then, if you have an idea ? – user451844 Sep 8 '17 at 1:53
• @Roddy MacPhee i am not sure this right way to approach this problem. – DMicheal Sep 8 '17 at 1:55
• so try it and find out ? or look at wikipedia etc. – user451844 Sep 8 '17 at 1:55
• @RoddyMacPhee Do you think you can help me? I am just not understanding. I know that 1000 elements would have 0-999 indexes for it, but the approach that I took doesn't make sense. – DMicheal Sep 8 '17 at 1:57
• how do you keep stepping that's a start. how would you get to 499th index. – user451844 Sep 8 '17 at 2:01

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.

• I understand how you are approaching your own example, but my array is a huger than that! Would I have to create a java program in order to figure out how many elements or comparison would have to be gone through to find those two elements? Can this be done using pen and paper? – DMicheal Sep 9 '17 at 14:45
• any element can be found in under $log_2(n)+1$ steps – user451844 Sep 9 '17 at 14:47
• Please read above the text I have written. – DMicheal Sep 9 '17 at 14:47
• I did for your comment for 1 million elements the worse case scenario is about 20 comparisons. – user451844 Sep 9 '17 at 14:50
• So it can be done using an algorithm formula; So if I do logbase 2 of 1000 + 1, I will see the number of comparisons it would have to make? That doesn't make sense to me. – DMicheal Sep 9 '17 at 14:52