binary search example with labeled indices
DESCRIPTION
-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…. - PowerPoint PPT PresentationTRANSCRIPT
Binary Search Example with Labeled IndicesBinary Search Example with Labeled Indices
-9 -2 -1 0 3 5 7 17 25
yellow == low
-9 -2 -1 0 3 5 7 17 25
Compare key to middle element…
-9 -2 -1 0 3 5 7 17 25
It can’t be in the first half; eliminate first half and compare to middle of what remains
-9 -2 -1 0 3 5 7 17 25
-9 -2 -1 0 3 5 7 17 -9
Can’t be 7 or anything greater; eliminate those elements and compare again
-9 -2 -1 0 3 5 7 17 -95 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.
blue == highgreen == middle
1 2 3 4 5 6 7 8 9