Skip to content

Commit b70a077

Browse files
authored
Sync binary-search docs with problem-specifications (#535)
The binary-search exercise has been overhauled as part of a project to make practice exercises more consistent and friendly. For more context, please see the discussion in the forum, as well as the pull request that updated the exercise in the problem-specifications repository: - https://forum.exercism.org/t/new-project-making-practice-exercises-more-consistent-and-human-across-exercism/3943 - exercism/problem-specifications#2220 ---- If you approve this pull request, I will eventually merge it. However, if you are happy with this change **please merge the pull request**, as it will get the changes into the hands of the students much more quickly.
1 parent 0195fb9 commit b70a077

File tree

2 files changed

+28
-26
lines changed

2 files changed

+28
-26
lines changed
Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,28 @@
11
# Instructions
22

3-
Implement a binary search algorithm.
3+
Your task is to implement a binary search algorithm.
44

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.
107

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+
```
1411

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:
1813

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.
2119

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:
2421

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]`.
2923

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.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Introduction
2+
3+
You have stumbled upon a group of mathematicians who are also singer-songwriters.
4+
They have written a song for each of their favorite numbers, and, as you can imagine, they have a lot of favorite numbers.
5+
6+
You are curious to hear the song for your favorite number, but with so many songs to wade through, finding the right song could take a while.
7+
Fortunately, they have organized their songs in a playlist sorted by the title — which is simply the number that the song is about.
8+
9+
You realize that you can use a binary search algorithm to quickly find a song given the title.

0 commit comments

Comments
 (0)