|
1 | 1 | # Instructions
|
2 | 2 |
|
3 |
| -Implement a binary search algorithm. |
| 3 | +Your task is to implement a binary search algorithm. |
4 | 4 |
|
5 |
| -Searching a sorted collection is a common task. A dictionary is a sorted |
6 |
| -list of word definitions. Given a word, one can find its definition. A |
7 |
| -telephone book is a sorted list of people's names, addresses, and |
8 |
| -telephone numbers. Knowing someone's name allows one to quickly find |
9 |
| -their telephone number and address. |
| 5 | +A binary search algorithm finds an item in a list by repeatedly splitting it in half, only keeping the half which contains the item we're looking for. |
| 6 | +It allows us to quickly narrow down the possible locations of our item until we find it, or until we've eliminated all possible locations. |
10 | 7 |
|
11 |
| -If the list to be searched contains more than a few items (a dozen, say) |
12 |
| -a binary search will require far fewer comparisons than a linear search, |
13 |
| -but it imposes the requirement that the list be sorted. |
| 8 | +```exercism/caution |
| 9 | +Binary search only works when a list has been sorted. |
| 10 | +``` |
14 | 11 |
|
15 |
| -In computer science, a binary search or half-interval search algorithm |
16 |
| -finds the position of a specified input value (the search "key") within |
17 |
| -an array sorted by key value. |
| 12 | +The algorithm looks like this: |
18 | 13 |
|
19 |
| -In each step, the algorithm compares the search key value with the key |
20 |
| -value of the middle element of the array. |
| 14 | +- Divide the sorted list in half and compare the middle element with the item we're looking for. |
| 15 | +- If the middle element is our item, then we're done. |
| 16 | +- If the middle element is greater than our item, we can eliminate that number and all the numbers **after** it. |
| 17 | +- If the middle element is less than our item, we can eliminate that number and all the numbers **before** it. |
| 18 | +- Repeat the process on the part of the list that we kept. |
21 | 19 |
|
22 |
| -If the keys match, then a matching element has been found and its index, |
23 |
| -or position, is returned. |
| 20 | +Here's an example: |
24 | 21 |
|
25 |
| -Otherwise, if the search key is less than the middle element's key, then |
26 |
| -the algorithm repeats its action on the sub-array to the left of the |
27 |
| -middle element or, if the search key is greater, on the sub-array to the |
28 |
| -right. |
| 22 | +Let's say we're looking for the number 23 in the following sorted list: `[4, 8, 12, 16, 23, 28, 32]`. |
29 | 23 |
|
30 |
| -If the remaining array to be searched is empty, then the key cannot be |
31 |
| -found in the array and a special "not found" indication is returned. |
32 |
| - |
33 |
| -A binary search halves the number of items to check with each iteration, |
34 |
| -so locating an item (or determining its absence) takes logarithmic time. |
35 |
| -A binary search is a dichotomic divide and conquer search algorithm. |
| 24 | +- We start by comparing 23 with the middle element, 16. |
| 25 | +- Since 23 is greater than 16, we can eliminate the left half of the list, leaving us with `[23, 28, 32]`. |
| 26 | +- We then compare 23 with the new middle element, 28. |
| 27 | +- Since 23 is less than 28, we can eliminate the right half of the list: `[23]`. |
| 28 | +- We've found our item. |
0 commit comments