How many elements would have to be looked (binary search) at in order to find an integer 5215 at element 499 and an integer 7282 at element 686 assuming it's binary search algorithm and total number of elements in the array are 1000?

How would I go about solving my problem? I know this algorithm checks to see if an search value (or the element that I want to locate) is in the middle first by dividing the total number of elements of the array in half; however, if it's not there, it checks the upper half of the array, but if it's not there, it goes back down again. Also, here is my proof that I did research this problem before attempting to ask the question which is embedded in the HTML code.

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 answers

0
Shivansh Singh On

Your question seems mysterious but I can help you with binary search algorithm if you need help in it.

Binary search algorithm works when your array is sorted that is in ascending or descending order.

This algorithm access your middle element of array at first iteration and if the element is not at n/2 in even case or n+1/2 in odd length case the algorithm will check if the search element is bigger or smaller , if it is smaller lower half is accessed and if it is bigger than upper half of array is accessed. This is the case when array is sorted in ascending order.

And for time complexity it gives you worst case scenario as O(log n). Where n is the number of elements in array that is length of array.

Hope I helped.