Download presentation
Presentation is loading. Please wait.
1
Binary Search Visualization
2
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 40 41 45 46 48 50 2 5 9 14 17 23 24 3353 57 62 66 71 72 76 80 i j GOAL: Find whether 48 exists in the given list of numbers.
3
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 40 41 45 46 48 50 2 5 9 14 17 23 24 3353 57 62 66 71 72 76 80 i j Search for 48 in the range of indices [ 0.. 21 ] 1.Find mid index: (0 + 21) / 2 = 10 2. At index 10 we have 45 3. 45 48 must be in the range [ 11.. 21 ] mid
4
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 40 41 45 46 48 50 2 5 9 14 17 23 24 3353 57 62 66 71 72 76 80 i j Search for 48 in the range of indices [ 11.. 21 ] 1.Find mid index: (11 + 21) / 2 = 16 2. At index 16 we have 62 3. 48 48 must be in the range [ 11.. 15 ] mid
5
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 40 41 45 46 48 50 2 5 9 14 17 23 24 3353 57 62 66 71 72 76 80 i j Search for 48 in the range of indices [ 11.. 15 ] 1.Find mid index: (11 + 15) / 2 = 13 2. At index 13 we have 50 3. 48 48 must be in the range [ 11.. 12 ] mid
6
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 40 41 45 46 48 50 2 5 9 14 17 23 24 3353 57 62 66 71 72 76 80 i j Search for 48 in the range of indices [ 11.. 12 ] 1.Find mid index: (11 + 12) / 2 = 11 2. At index 11 we have 46 3. 46 48 must be in the range [ 12.. 12 ]
7
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 40 41 45 46 48 50 2 5 9 14 17 23 24 3353 57 62 66 71 72 76 80 Search for 48 in the range of indices [ 12.. 12 ] 1.Find mid index: (12 + 12) / 2 = 12 2. At index 12 we have 48 3. 48 == 48 ! report “success” j i mid 22 elements in the array --- only 5 comparisons made!
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.