Skip to content

Commit 49303f6

Browse files
Sync binary-search docs with problem-specifications (#1314)
Co-authored-by: Derk-Jan Karrenbeld <[email protected]>
1 parent 8fd4678 commit 49303f6

File tree

2 files changed

+34
-26
lines changed

2 files changed

+34
-26
lines changed
Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,30 @@
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+
<!-- prettier-ignore -->
9+
~~~~exercism/caution
10+
Binary search only works when a list has been sorted.
11+
~~~~
1412

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.
13+
The algorithm looks like this:
1814

19-
In each step, the algorithm compares the search key value with the key
20-
value of the middle element of the array.
15+
- Find the middle element of a sorted list and compare it with the item we're looking for.
16+
- If the middle element is our item, then we're done!
17+
- If the middle element is greater than our item, we can eliminate that element and all the elements **after** it.
18+
- If the middle element is less than our item, we can eliminate that element and all the elements **before** it.
19+
- If every element of the list has been eliminated then the item is not in the list.
20+
- Otherwise, repeat the process on the part of the list that has not been eliminated.
2121

22-
If the keys match, then a matching element has been found and its index,
23-
or position, is returned.
22+
Here's an example:
2423

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.
24+
Let's say we're looking for the number 23 in the following sorted list: `[4, 8, 12, 16, 23, 28, 32]`.
2925

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.
26+
- We start by comparing 23 with the middle element, 16.
27+
- Since 23 is greater than 16, we can eliminate the left half of the list, leaving us with `[23, 28, 32]`.
28+
- We then compare 23 with the new middle element, 28.
29+
- Since 23 is less than 28, we can eliminate the right half of the list: `[23]`.
30+
- We've found our item.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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 (like [0][zero] or [73][seventy-three] or [6174][kaprekars-constant]).
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.
10+
11+
[zero]: https://en.wikipedia.org/wiki/0
12+
[seventy-three]: https://en.wikipedia.org/wiki/73_(number)
13+
[kaprekars-constant]: https://en.wikipedia.org/wiki/6174_(number)

0 commit comments

Comments
 (0)