10 likes | 73 Views
Learn how Binary Search works step-by-step with labeled indices. Compare key to middle element, eliminate half, and find the key efficiently.
E N D
-9 -9 -9 -9 -9 -9 -2 -2 -2 -2 -2 -2 -1 -1 -1 -1 -1 -1 0 0 0 0 0 0 3 3 3 3 3 3 5 5 5 5 5 5 7 7 7 7 7 7 17 17 17 17 17 17 25 25 25 25 -9 -9 Compare key to middle element… It can’t be in the first half; eliminate first half and compare to middle of what remains Can’t be 7 or anything greater; eliminate those elements and compare again Binary Search Example with Labeled Indices blue == high yellow == low green == middle 1 2 3 4 5 6 7 8 9 5 is now the only thing remaining in the array. It is thus also the middle. Since the middle equals the key, we’ve successfully found our key.