|
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. |
6 |
| -A dictionary is a sorted list of word definitions. |
7 |
| -Given a word, one can find its definition. |
8 |
| -A telephone book is a sorted list of people's names, addresses, and telephone numbers. |
9 |
| -Knowing someone's name allows one to quickly find 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) a binary search will require far fewer comparisons than a linear search, 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 | +``` |
12 | 11 |
|
13 |
| -In computer science, a binary search or half-interval search algorithm finds the position of a specified input value (the search "key") within an array sorted by key value. |
| 12 | +The algorithm looks like this: |
14 | 13 |
|
15 |
| -In each step, the algorithm compares the search key value with the key 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. |
16 | 19 |
|
17 |
| -If the keys match, then a matching element has been found and its index, or position, is returned. |
| 20 | +Here's an example: |
18 | 21 |
|
19 |
| -Otherwise, if the search key is less than the middle element's key, then the algorithm repeats its action on the sub-array to the left of the middle element or, if the search key is greater, on the sub-array to the right. |
| 22 | +Let's say we're looking for the number 23 in the following sorted list: `[4, 8, 12, 16, 23, 28, 32]`. |
20 | 23 |
|
21 |
| -If the remaining array to be searched is empty, then the key cannot be found in the array and a special "not found" indication is returned. |
22 |
| - |
23 |
| -A binary search halves the number of items to check with each iteration, so locating an item (or determining its absence) takes logarithmic time. |
24 |
| -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