aboutsummaryrefslogtreecommitdiff
path: root/rust/binary-search/README.md
diff options
context:
space:
mode:
authorFederico Igne <git@federicoigne.com>2021-01-02 17:58:32 +0000
committerFederico Igne <git@federicoigne.com>2021-11-03 18:55:08 +0000
commite7e054e21fd530fa6873b7aedd1fc4d4cfe5eb29 (patch)
tree66933e429dbcb0e0dff68cc23cd1ef9430f12eed /rust/binary-search/README.md
parentfdde4fcd039210d7378c3f31ec9372396b1464a9 (diff)
downloadexercism-e7e054e21fd530fa6873b7aedd1fc4d4cfe5eb29.tar.gz
exercism-e7e054e21fd530fa6873b7aedd1fc4d4cfe5eb29.zip
[rust] Binary Search
Diffstat (limited to 'rust/binary-search/README.md')
-rw-r--r--rust/binary-search/README.md160
1 files changed, 160 insertions, 0 deletions
diff --git a/rust/binary-search/README.md b/rust/binary-search/README.md
new file mode 100644
index 0000000..d447602
--- /dev/null
+++ b/rust/binary-search/README.md
@@ -0,0 +1,160 @@
1# Binary Search
2
3Implement a binary search algorithm.
4
5Searching a sorted collection is a common task. A dictionary is a sorted
6list of word definitions. Given a word, one can find its definition. A
7telephone book is a sorted list of people's names, addresses, and
8telephone numbers. Knowing someone's name allows one to quickly find
9their telephone number and address.
10
11If the list to be searched contains more than a few items (a dozen, say)
12a binary search will require far fewer comparisons than a linear search,
13but it imposes the requirement that the list be sorted.
14
15In computer science, a binary search or half-interval search algorithm
16finds the position of a specified input value (the search "key") within
17an array sorted by key value.
18
19In each step, the algorithm compares the search key value with the key
20value of the middle element of the array.
21
22If the keys match, then a matching element has been found and its index,
23or position, is returned.
24
25Otherwise, if the search key is less than the middle element's key, then
26the algorithm repeats its action on the sub-array to the left of the
27middle element or, if the search key is greater, on the sub-array to the
28right.
29
30If the remaining array to be searched is empty, then the key cannot be
31found in the array and a special "not found" indication is returned.
32
33A binary search halves the number of items to check with each iteration,
34so locating an item (or determining its absence) takes logarithmic time.
35A binary search is a dichotomic divide and conquer search algorithm.
36
37## Restrictions
38
39Rust provides in its standard library already a
40[binary search function](https://doc.rust-lang.org/std/primitive.slice.html#method.binary_search).
41For this exercise you should not use this function but just other basic tools instead.
42
43## Hints
44
45[Slices](https://doc.rust-lang.org/book/2018-edition/ch04-03-slices.html) have additionally to
46the normal element access via indexing (slice[index]) many useful functions like
47[split_at](https://doc.rust-lang.org/std/primitive.slice.html#method.split_at) or [getting
48subslices](https://doc.rust-lang.org/std/primitive.slice.html#method.get) (slice[start..end]).
49
50You can solve this exercise by just using boring old element access via indexing, but maybe the
51other provided functions can make your code cleaner and safer.
52
53## For bonus points
54
55Did you get the tests passing and the code clean? If you want to, there
56are some additional things you could try.
57
58- Currently your find function will probably only work for slices of numbers,
59 but the Rust type system is flexible enough to create a find function which
60 works on all slices which contains elements which can be ordered.
61- Additionally this find function can work not only on slices, but at the
62 same time also on a Vec or an Array.
63
64To run the bonus tests, remove the `#[ignore]` flag and execute the tests with
65the `generic` feature, like this:
66
67```bash
68$ cargo test --features generic
69```
70
71Then please share your thoughts in a comment on the submission. Did this
72experiment make the code better? Worse? Did you learn anything from it?
73
74### Hints for Bonus Points
75
76- To get your function working with all kind of elements which can be ordered,
77 have a look at the [Ord Trait](https://doc.rust-lang.org/std/cmp/trait.Ord.html).
78- To get your function working directly on Vec and Array, you can use the
79 [AsRef Trait](https://doc.rust-lang.org/std/convert/trait.AsRef.html)
80
81
82## Rust Installation
83
84Refer to the [exercism help page][help-page] for Rust installation and learning
85resources.
86
87## Writing the Code
88
89Execute the tests with:
90
91```bash
92$ cargo test
93```
94
95All but the first test have been ignored. After you get the first test to
96pass, open the tests source file which is located in the `tests` directory
97and remove the `#[ignore]` flag from the next test and get the tests to pass
98again. Each separate test is a function with `#[test]` flag above it.
99Continue, until you pass every test.
100
101If you wish to run all ignored tests without editing the tests source file, use:
102
103```bash
104$ cargo test -- --ignored
105```
106
107To run a specific test, for example `some_test`, you can use:
108
109```bash
110$ cargo test some_test
111```
112
113If the specific test is ignored use:
114
115```bash
116$ cargo test some_test -- --ignored
117```
118
119To learn more about Rust tests refer to the [online test documentation][rust-tests]
120
121Make sure to read the [Modules][modules] chapter if you
122haven't already, it will help you with organizing your files.
123
124## Further improvements
125
126After you have solved the exercise, please consider using the additional utilities, described in the [installation guide](https://exercism.io/tracks/rust/installation), to further refine your final solution.
127
128To format your solution, inside the solution directory use
129
130```bash
131cargo fmt
132```
133
134To see, if your solution contains some common ineffective use cases, inside the solution directory use
135
136```bash
137cargo clippy --all-targets
138```
139
140## Submitting the solution
141
142Generally you should submit all files in which you implemented your solution (`src/lib.rs` in most cases). If you are using any external crates, please consider submitting the `Cargo.toml` file. This will make the review process faster and clearer.
143
144## Feedback, Issues, Pull Requests
145
146The [exercism/rust](https://github.com/exercism/rust) repository on GitHub is the home for all of the Rust exercises. If you have feedback about an exercise, or want to help implement new exercises, head over there and create an issue. Members of the rust track team are happy to help!
147
148If you want to know more about Exercism, take a look at the [contribution guide](https://github.com/exercism/docs/blob/master/contributing-to-language-tracks/README.md).
149
150[help-page]: https://exercism.io/tracks/rust/learning
151[modules]: https://doc.rust-lang.org/book/ch07-02-defining-modules-to-control-scope-and-privacy.html
152[cargo]: https://doc.rust-lang.org/book/ch14-00-more-about-cargo.html
153[rust-tests]: https://doc.rust-lang.org/book/ch11-02-running-tests.html
154
155## Source
156
157Wikipedia [http://en.wikipedia.org/wiki/Binary_search_algorithm](http://en.wikipedia.org/wiki/Binary_search_algorithm)
158
159## Submitting Incomplete Solutions
160It's possible to submit an incomplete solution so you can see how others have completed the exercise.