| Crates.io | thinkrust_algorithms |
| lib.rs | thinkrust_algorithms |
| version | 0.1.2 |
| created_at | 2025-01-29 05:18:07.320197+00 |
| updated_at | 2025-01-29 05:23:48.495138+00 |
| description | Basic Algorithms: Binary Search and Find Max from an array |
| homepage | https://github.com/thinkphp/crates_algorithms |
| repository | https://github.com/thinkphp/crates_algorithms |
| max_upload_size | |
| id | 1534375 |
| size | 7,568 |
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.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 your_crate_name::binary_search;
let items = vec![16, 32, 64, 128, 256, 512, 1024];
let result = binary_search(items, 256);
match result {
Some(index) => println!("Item found at index: {}", index),
None => println!("Item not found"),
}
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 your_crate_name::find_max_recursive;
let items = vec![8, 12, 128, 1, 90, -1, 32];
let max_value = find_max_recursive(&items);
println!("The maximum value is: {}", max_value);
binary_searchpub fn binary_search<T>(list: Vec<T>, item: T) -> Option<usize>
where
T: std::cmp::Ord,
list: A vector of elements that must be sorted in ascending order.item: The element to search for in the vector.Some(usize): The index of the item in the list if found.None: If the item is not found in the list.find_max_recursivepub fn find_max_recursive(coll: &Vec<i32>) -> i32
coll: A reference to a vector of integers (Vec<i32>) to find the maximum value in.i32: The maximum value in the vector. Returns 0 if the vector is empty.This crate includes the following tests:
binary_search:None.find_max_recursive:0.To run the tests for both functions, use the following command:
cargo test
This crate is licensed under the MIT License.