Binary Search and Max Finder Crate
This crate provides two utility functions:
binary_search
: A function that performs a binary search to find the index of an item in a sorted vector.find_max_recursive
: A recursive function to find the maximum value in a vector of integers.
Features
- Implements a binary search algorithm for sorted collections.
- Implements a recursive algorithm to find the maximum value in a collection of integers.
- Provides tests to ensure correctness and handle edge cases.
Usage
Binary Search
To use the binary_search
function, you can call it with a sorted vector and the item to search for. It returns the index of the item if found, or None
if not.
use binary_search;
let items = vec!;
let result = binary_search;
match result
Find Maximum Value Recursively
To use the find_max_recursive
function, call it with a vector of integers. It returns the maximum value in the vector or 0
if the vector is empty.
use find_max_recursive;
let items = vec!;
let max_value = find_max_recursive;
println!;
Function Details
binary_search
Parameters:
list
: A vector of elements that must be sorted in ascending order.item
: The element to search for in the vector.
Returns:
Some(usize)
: The index of the item in the list if found.None
: If the item is not found in the list.
find_max_recursive
Parameters:
coll
: A reference to a vector of integers (Vec<i32>
) to find the maximum value in.
Returns:
i32
: The maximum value in the vector. Returns0
if the vector is empty.
Testing
This crate includes the following tests:
For binary_search
:
- returns_none_if_items_is_empty: Ensures that an empty list returns
None
. - doesnt_finds_if_collection_is_not_sorted: Tests that unsorted lists do not return a valid result.
- finds_index_of_element: Verifies that the index of an element is found in a sorted list.
For find_max_recursive
:
- finds_the_greatest_value: Verifies that the maximum value is correctly found in a non-empty vector.
- returns_zero_if_empty_vec_is_provided: Ensures that an empty vector returns
0
.
Running Tests
To run the tests for both functions, use the following command:
License
This crate is licensed under the MIT License.