Hi,
Let there be sorted list of 1024 elements.
Since 1024 is a power of 2, there does not exist a true mid, which separates the list into two equal lists.
Such as 3 in this list - [1,2,3,4,5], here 3 is mid and separates into two lists([1,2].[4,5]) if the element needed to be found is not the mid.
While dividing the list into two lists,
We can consider mid as the last element in left/first list or as first element in right/second list.
Let us take mid as last element in the left/first list.
Also, the question assumes that the target element exists in the list, if it doesn't exist it results in an error/exception case,
where the algorithm fails to find an element.
Let us put the element we need to search at index 1, to get the maximum possible iterations.
For simplicity, it is considered here that indexing beings from 1.
So, [1,2,3,4......1024] becomes
L=[1...512], R=[513....1024], Mid = 512
We do a comparison here with mid, Comparisons = 1
L = [1...256], R = [257...512], Mid = 256
Again, a comparison here with mid, Comparisons = 2
L = [1...128], R = [129...256], Mid = 128
Again, a comparison here with mid, Comparisons = 3
L = [1...64], R = [65...128], Mid = 64
Again, a comparison here with mid, Comparisons = 4
L = [1...32], R = [33...64], Mid = 32
Again, a comparison here with mid, Comparisons = 5
L = [1...16], R = [17...32], Mid = 16
Again, a comparison here with mid, Comparisons = 6
L = [1...8], R = [9...16], Mid = 8
Again, a comparison here with mid, Comparisons = 7
L = [1...4], R = [5...8], Mid = 4
Again, a comparison here with mid, Comparisons = 8
L = [1...2], R = [3...4], Mid = 2
Again, a comparison here with mid, Comparisons = 9
L = [1], R = [2], Mid = 1
Final comparison here with mid, Comparisons = 10
Element Found.
Even if the element to be found is at index 2, we would still require only 10 comparisons.
Thanks,
TA (The Joy of Computing using Python)