binary search example with labeled indices

1
Binary Search Example with Binary Search Example with Labeled Indices Labeled Indices -9 -2 -1 0 3 5 7 17 25 yellow == low -9 -2 -1 0 3 5 7 17 25 ompare key to middle element… -9 -2 -1 0 3 5 7 17 25 n the first half; eliminate first half and compare to middle -9 -2 -1 0 3 5 7 17 25 -9 -2 -1 0 3 5 7 17 -9 7 or anything greater; eliminate those elements and compare a -9 -2 -1 0 3 5 7 17 -9 only thing remaining in the array. It is thus also the middl quals the key, we’ve successfully found our key. blue == high green == middle 3 4 5 6 7

Upload: ulric-baker

Post on 31-Dec-2015

9 views

Category:

Documents


0 download

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 Presentation

TRANSCRIPT

Page 1: Binary Search Example with Labeled Indices

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