-
-
Notifications
You must be signed in to change notification settings - Fork 19
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
11 changed files
with
68 additions
and
35 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,29 @@ | ||
# Description | ||
# Instructions | ||
|
||
Implement a binary search algorithm. | ||
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
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,11 +1,19 @@ | ||
{ | ||
"authors": ["natanaelsirqueira"], | ||
"authors": [ | ||
"natanaelsirqueira" | ||
], | ||
"files": { | ||
"solution": ["secret_handshake.v"], | ||
"test": ["run_test.v"], | ||
"example": [".meta/example.v"] | ||
"solution": [ | ||
"secret_handshake.v" | ||
], | ||
"test": [ | ||
"run_test.v" | ||
], | ||
"example": [ | ||
".meta/example.v" | ||
] | ||
}, | ||
"blurb": "Given a decimal number, convert it to the appropriate sequence of events for a secret handshake.", | ||
"source": "Bert, in Mary Poppins", | ||
"source_url": "https://www.imdb.com/title/tt0058331/quotes/qt0437047" | ||
"source_url": "https://www.imdb.com/title/tt0058331/quotes/?item=qt0437047" | ||
} |
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