Upcoming SlideShare
×

# Binary search: illustrated step-by-step walk through

1,371
-1

Published on

A step-by-step illustration of Binary Search to help you walk through a series of operations. Illustration is accompanied by actual code with bold line indicating the current operation.

https://github.com/yoshiwatanabe/Algorithms/blob/master/Finding/BinarySearch.cs

Published in: Technology, Spiritual
0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total Views
1,371
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
40
0
Likes
0
Embeds 0
No embeds

No notes for slide

### Binary search: illustrated step-by-step walk through

1. 1. Binary Search Illustrated walk through
2. 2. Iterative binary search int begin = 0; int last = array.Length - 1; int mid = 0; while (begin <= last) { mid = (begin + last) / 2; if (array[mid] < x) { begin = mid + 1; } else if (array[mid] > x) { last = mid - 1; } else { return mid; } } return -1; Part #1 Initialize pointers Part #2 Search
3. 3. begin last mid x 4 This is what we search for [0] [1] [2] [3] 2 4 5 6 Let’s look for 4
4. 4. begin 0 x last mid 3 0 [0] [1] [2] [3] 2 4 5 6 int begin = 0; int last = array.Length - 1; int mid = 0; 4
5. 5. begin 0 x 0 <= 3 is true mid last 3 0 [0] [1] [2] [3] 2 4 5 6 while (begin <= last) { mid = (begin + last) / 2; if (array[mid] < x) { begin = mid + 1; } else if (array[mid] > x) { last = mid - 1; } else { return mid; } } return -1; 4
6. 6. begin 0 x last (0+3)/2 = 1 mid 3 1 [0] [1] [2] [3] 2 4 5 6 while (begin <= last) { mid = (begin + last) / 2; if (array[mid] < x) { begin = mid + 1; } else if (array[mid] > x) { last = mid - 1; } else { return mid; } } return -1; 4
7. 7. begin 0 x last mid 4 3 1 [0] [1] [2] [3] 2 4 5 6 while (begin <= last) { mid = (begin + last) / 2; if (array[mid] < x) { begin = mid + 1; } else if (array[mid] > x) { last = mid - 1; } else { return mid; } } return -1; 4<4 is false
8. 8. begin 0 x last mid 4 3 1 [0] [1] [2] [3] 2 4 5 6 while (begin <= last) { mid = (begin + last) / 2; if (array[mid] < x) { begin = mid + 1; } else if ( array[mid] > x) { last = mid - 1; } else { return mid; } } return -1; 4>4 is false
9. 9. begin 0 x last mid 4 3 1 [0] [1] [2] [3] 2 4 5 6 while (begin <= last) { mid = (begin + last) / 2; if (array[mid] < x) { begin = mid + 1; } else if (array[mid] > x) { last = mid - 1; } else { return mid; } } We found x=4 at index 1! return -1;
10. 10. begin last mid x 5 This is what we search for [0] [1] [2] [3] 2 4 5 6 Let’s look for 5
11. 11. begin 0 x last mid 3 0 [0] [1] [2] [3] 2 4 5 6 int begin = 0; int last = array.Length - 1; int mid = 0; 5
12. 12. begin 0 x 0 <= 3 is true mid last 3 0 [0] [1] [2] [3] 2 4 5 6 while (begin <= last) { mid = (begin + last) / 2; if (array[mid] < x) { begin = mid + 1; } else if (array[mid] > x) { last = mid - 1; } else { return mid; } } return -1; 5
13. 13. begin 0 x last (0+3)/2 = 1 mid 3 1 [0] [1] [2] [3] 2 4 5 6 while (begin <= last) { mid = (begin + last) / 2; if (array[mid] < x) { begin = mid + 1; } else if (array[mid] > x) { last = mid - 1; } else { return mid; } } return -1; 5
14. 14. begin 0 x last mid 5 3 1 [0] [1] [2] [3] 2 4 5 6 while (begin <= last) { mid = (begin + last) / 2; if (array[mid] < x) { begin = mid + 1; } else if (array[mid] > x) { last = mid - 1; } else { return mid; } } return -1; 4<5 is true
15. 15. begin 2 x last mid 3 1 [0] [1] [2] [3] 2 4 5 6 while (begin <= last) { mid = (begin + last) / 2; if (array[mid] < x) { begin = mid + 1; } else if (array[mid] > x) { last = mid - 1; } else { return mid; } } return -1; 5
16. 16. begin 2 <= 3 is true 2 x last mid 3 1 [0] [1] [2] [3] 2 4 5 6 while (begin <= last) { mid = (begin + last) / 2; if (array[mid] < x) { begin = mid + 1; } else if (array[mid] > x) { last = mid - 1; } else { return mid; } } return -1; 5
17. 17. begin 2 x last (2+3)/2 =2 mid 3 2 [0] [1] [2] [3] 2 4 5 6 while (begin <= last) { mid = (begin + last) / 2; if (array[mid] < x) { begin = mid + 1; } else if (array[mid] > x) { last = mid - 1; } else { return mid; } } return -1; 5
18. 18. begin 2 x last mid 3 2 [0] [1] [2] [3] 2 4 5 6 while (begin <= last) { mid = (begin + last) / 2; if (array[mid] < x) { begin = mid + 1; } else if (array[mid] > x) { last = mid - 1; } else { return mid; } } return -1; 5 5<5 is false
19. 19. begin 2 x last mid 3 2 [0] [1] [2] [3] 2 4 5 6 while (begin <= last) { mid = (begin + last) / 2; if (array[mid] < x) { begin = mid + 1; } else if ( array[mid] > x) { last = mid - 1; } else { return mid; } } return -1; 5 5>5 is false
20. 20. begin 2 x last mid 5 3 2 [0] [1] [2] [3] 2 4 5 6 while (begin <= last) { mid = (begin + last) / 2; if (array[mid] < x) { begin = mid + 1; } else if (array[mid] > x) { last = mid - 1; } else { return mid; } } We found x=5 at index 2! return -1;
21. 21. begin last mid x 6 This is what we search for [0] [1] [2] [3] 2 4 5 6 Let’s look for 6
22. 22. begin 0 x last mid 3 0 [0] [1] [2] [3] 2 4 5 6 int begin = 0; int last = array.Length - 1; int mid = 0; 6
23. 23. begin 0 x 0 <= 3 is true mid last 3 0 [0] [1] [2] [3] 2 4 5 6 while (begin <= last) { mid = (begin + last) / 2; if (array[mid] < x) { begin = mid + 1; } else if (array[mid] > x) { last = mid - 1; } else { return mid; } } return -1; 6
24. 24. begin 0 x last (0+3)/2 = 1 mid 3 1 [0] [1] [2] [3] 2 4 5 6 while (begin <= last) { mid = (begin + last) / 2; if (array[mid] < x) { begin = mid + 1; } else if (array[mid] > x) { last = mid - 1; } else { return mid; } } return -1; 6
25. 25. begin 0 x last mid 6 3 1 [0] [1] [2] [3] 2 4 5 6 while (begin <= last) { mid = (begin + last) / 2; if (array[mid] < x) { begin = mid + 1; } else if (array[mid] > x) { last = mid - 1; } else { return mid; } } return -1; 4<6 is true
26. 26. begin 2 x last mid 3 1 [0] [1] [2] [3] 2 4 5 6 while (begin <= last) { mid = (begin + last) / 2; if (array[mid] < x) { begin = mid + 1; } else if (array[mid] > x) { last = mid - 1; } else { return mid; } } return -1; 6
27. 27. begin 2 <= 3 is true 2 x last mid 3 1 [0] [1] [2] [3] 2 4 5 6 while (begin <= last) { mid = (begin + last) / 2; if (array[mid] < x) { begin = mid + 1; } else if (array[mid] > x) { last = mid - 1; } else { return mid; } } return -1; 6
28. 28. begin 2 x last (2+3)/2 =2 mid 3 2 [0] [1] [2] [3] 2 4 5 6 while (begin <= last) { mid = (begin + last) / 2; if (array[mid] < x) { begin = mid + 1; } else if (array[mid] > x) { last = mid - 1; } else { return mid; } } return -1; 6
29. 29. begin 2 x last mid 3 2 [0] [1] [2] [3] 2 4 5 6 while (begin <= last) { mid = (begin + last) / 2; if (array[mid] < x) { begin = mid + 1; } else if (array[mid] > x) { last = mid - 1; } else { return mid; } } return -1; 6 5<6 is true
30. 30. begin last mid 3 3 2 [0] [1] [2] [3] 2 4 5 6 while (begin <= last) { mid = (begin + last) / 2; if (array[mid] < x) { begin = mid + 1; } else if (array[mid] > x) { last = mid - 1; } else { return mid; } } return -1; x 6
31. 31. begin last 3 <= 3 is true mid 3 3 2 [0] [1] [2] [3] 2 4 5 6 while (begin <= last) { mid = (begin + last) / 2; if (array[mid] < x) { begin = mid + 1; } else if (array[mid] > x) { last = mid - 1; } else { return mid; } } return -1; x 6
32. 32. begin last 3 mid (3+3)/2 = 3 3 3 [0] [1] [2] [3] 2 4 5 6 while (begin <= last) { mid = (begin + last) / 2; if (array[mid] < x) { begin = mid + 1; } else if (array[mid] > x) { last = mid - 1; } else { return mid; } } return -1; x 6
33. 33. begin 3 last 3 mid 3 [0] [1] [2] [3] 2 4 5 6 while (begin <= last) { mid = (begin + last) / 2; if (array[mid] < x) { begin = mid + 1; } else if (array[mid] > x) { last = mid - 1; } else { return mid; } } return -1; x 6 6<6 is false
34. 34. begin 3 last 3 mid 3 [0] [1] [2] [3] 2 4 5 6 while (begin <= last) { mid = (begin + last) / 2; if (array[mid] < x) { begin = mid + 1; } else if ( array[mid] > x) { last = mid - 1; } else { return mid; } } return -1; x 6 6>6 is false
35. 35. begin 3 last 3 mid 3 [0] [1] [2] 4 5 6 [3] 2 x 6 while (begin <= last) { mid = (begin + last) / 2; if (array[mid] < x) { begin = mid + 1; } else if (array[mid] > x) { last = mid - 1; } else { return mid; } } We found x=6 at index 3! return -1;
1. #### A particular slide catching your eye?

Clipping is a handy way to collect important slides you want to go back to later.