-
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
49 changed files
with
521 additions
and
315 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,29 @@ | ||
# Instructions | ||
|
||
Implement a binary search algorithm. Do not use any COBOL proprietary | ||
search routines (eg. SEARCH). | ||
Your task is to implement a binary search algorithm. | ||
|
||
Searching a sorted collection is a common task. A dictionary is a sorted | ||
list of word definitions. Given a word, one can find its definition. A | ||
telephone book is a sorted list of people's names, addresses, and | ||
telephone numbers. Knowing someone's name allows one to quickly find | ||
their telephone number and address. | ||
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. | ||
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. | ||
|
||
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. | ||
~~~~exercism/caution | ||
Binary search only works when a list has been sorted. | ||
~~~~ | ||
|
||
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. | ||
The algorithm looks like this: | ||
|
||
In each step, the algorithm compares the search key value with the key | ||
value of the middle element of the array. | ||
- Find the middle element of a _sorted_ list and compare it with the item we're looking for. | ||
- If the middle element is our item, then we're done! | ||
- If the middle element is greater than our item, we can eliminate that element and all the elements **after** it. | ||
- If the middle element is less than our item, we can eliminate that element and all the elements **before** it. | ||
- If every element of the list has been eliminated then the item is not in the list. | ||
- Otherwise, repeat the process on the part of the list that has not been eliminated. | ||
|
||
If the keys match, then a matching element has been found and its index, | ||
or position, is returned. | ||
Here's an example: | ||
|
||
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. | ||
Let's say we're looking for the number 23 in the following sorted list: `[4, 8, 12, 16, 23, 28, 32]`. | ||
|
||
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. | ||
|
||
A binary search halves the number of items to check with each iteration, | ||
so locating an item (or determining its absence) takes logarithmic time. | ||
A binary search is a dichotomic divide and conquer search algorithm. | ||
- We start by comparing 23 with the middle element, 16. | ||
- Since 23 is greater than 16, we can eliminate the left half of the list, leaving us with `[23, 28, 32]`. | ||
- We then compare 23 with the new middle element, 28. | ||
- Since 23 is less than 28, we can eliminate the right half of the list: `[23]`. | ||
- We've found our item. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# Introduction | ||
|
||
You have stumbled upon a group of mathematicians who are also singer-songwriters. | ||
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]). | ||
|
||
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. | ||
Fortunately, they have organized their songs in a playlist sorted by the title — which is simply the number that the song is about. | ||
|
||
You realize that you can use a binary search algorithm to quickly find a song given the title. | ||
|
||
[zero]: https://en.wikipedia.org/wiki/0 | ||
[seventy-three]: https://en.wikipedia.org/wiki/73_(number) | ||
[kaprekars-constant]: https://en.wikipedia.org/wiki/6174_(number) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,58 @@ | ||
# Description | ||
# Instructions | ||
|
||
A circular buffer, cyclic buffer or ring buffer is a data structure that | ||
uses a single, fixed-size buffer as if it were connected end-to-end. | ||
A circular buffer, cyclic buffer or ring buffer is a data structure that uses a single, fixed-size buffer as if it were connected end-to-end. | ||
|
||
A circular buffer first starts empty and of some predefined length. For | ||
example, this is a 7-element buffer: | ||
<!-- prettier-ignore --> | ||
[ ][ ][ ][ ][ ][ ][ ] | ||
A circular buffer first starts empty and of some predefined length. | ||
For example, this is a 7-element buffer: | ||
|
||
Assume that a 1 is written into the middle of the buffer (exact starting | ||
location does not matter in a circular buffer): | ||
<!-- prettier-ignore --> | ||
[ ][ ][ ][1][ ][ ][ ] | ||
```text | ||
[ ][ ][ ][ ][ ][ ][ ] | ||
``` | ||
|
||
Then assume that two more elements are added — 2 & 3 — which get | ||
appended after the 1: | ||
<!-- prettier-ignore --> | ||
[ ][ ][ ][1][2][3][ ] | ||
Assume that a 1 is written into the middle of the buffer (exact starting location does not matter in a circular buffer): | ||
|
||
If two elements are then removed from the buffer, the oldest values | ||
inside the buffer are removed. The two elements removed, in this case, | ||
are 1 & 2, leaving the buffer with just a 3: | ||
<!-- prettier-ignore --> | ||
[ ][ ][ ][ ][ ][3][ ] | ||
```text | ||
[ ][ ][ ][1][ ][ ][ ] | ||
``` | ||
|
||
Then assume that two more elements are added — 2 & 3 — which get appended after the 1: | ||
|
||
```text | ||
[ ][ ][ ][1][2][3][ ] | ||
``` | ||
|
||
If two elements are then removed from the buffer, the oldest values inside the buffer are removed. | ||
The two elements removed, in this case, are 1 & 2, leaving the buffer with just a 3: | ||
|
||
```text | ||
[ ][ ][ ][ ][ ][3][ ] | ||
``` | ||
|
||
If the buffer has 7 elements then it is completely full: | ||
<!-- prettier-ignore --> | ||
[5][6][7][8][9][3][4] | ||
|
||
When the buffer is full an error will be raised, alerting the client | ||
that further writes are blocked until a slot becomes free. | ||
|
||
When the buffer is full, the client can opt to overwrite the oldest | ||
data with a forced write. In this case, two more elements — A & B — | ||
are added and they overwrite the 3 & 4: | ||
<!-- prettier-ignore --> | ||
[5][6][7][8][9][A][B] | ||
|
||
3 & 4 have been replaced by A & B making 5 now the oldest data in the | ||
buffer. Finally, if two elements are removed then what would be | ||
returned is 5 & 6 yielding the buffer: | ||
<!-- prettier-ignore --> | ||
[ ][ ][7][8][9][A][B] | ||
|
||
Because there is space available, if the client again uses overwrite | ||
to store C & D then the space where 5 & 6 were stored previously will | ||
be used not the location of 7 & 8. 7 is still the oldest element and | ||
the buffer is once again full. | ||
<!-- prettier-ignore --> | ||
[C][D][7][8][9][A][B] | ||
|
||
```text | ||
[5][6][7][8][9][3][4] | ||
``` | ||
|
||
When the buffer is full an error will be raised, alerting the client that further writes are blocked until a slot becomes free. | ||
|
||
When the buffer is full, the client can opt to overwrite the oldest data with a forced write. | ||
In this case, two more elements — A & B — are added and they overwrite the 3 & 4: | ||
|
||
```text | ||
[5][6][7][8][9][A][B] | ||
``` | ||
|
||
3 & 4 have been replaced by A & B making 5 now the oldest data in the buffer. | ||
Finally, if two elements are removed then what would be returned is 5 & 6 yielding the buffer: | ||
|
||
```text | ||
[ ][ ][7][8][9][A][B] | ||
``` | ||
|
||
Because there is space available, if the client again uses overwrite to store C & D then the space where 5 & 6 were stored previously will be used not the location of 7 & 8. | ||
7 is still the oldest element and the buffer is once again full. | ||
|
||
```text | ||
[C][D][7][8][9][A][B] | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.