1 unstable release
0.1.0 | Sep 9, 2023 |
---|
#2614 in Algorithms
11KB
237 lines
SearchSort
A Rust trait implementing a Binary Search and the Quick Sort algorithm. Currently, they are implemented for
Vec<T>
types but can be expanded to other collection types.
Usage
cargo add searchsort
Examples
use searchsort::SearchSort;
let arr = vec![4, 82, 4, 32, 3, 20, 3, 2, 2, 9, 8, 7, 5, 0];
let find = 5;
assert_eq!(arr.find_me(find, 0, arr.len()-1), Some(12));
let mut arr = vec![3, 1, 4, 1, 5, 9, 2, 6, 5];
arr.quicksort();
assert_eq!(arr, [1, 1, 2, 3, 4, 5, 5, 6, 9]);
Why
This was my first time implementing the Binary Search and Quick Sort algorithms. I have a feeling this may not be the last time.
lib.rs
:
This crate provides a trait SearchSort
that defines methods for searching and sorting.
The SearchSort
trait provides a method find_me
that finds the first occurrence of an element in a slice between a start and end index. It returns Some(index)
if found, otherwise None
.
The crate also provides an implementation of the SearchSort
trait for the Vec<T>
type, which allows you to use the find_me
method on vectors.
Additionally, the crate provides a method quicksort
that sorts a mutable slice in-place using the quicksort algorithm.
The crate also includes tests and benchmarks for the find_me
and quicksort
methods.
Examples
use searchsort::SearchSort;
let arr = vec![4, 82, 4, 32, 3, 20, 3, 2, 2, 9, 8, 7, 5, 0];
let find = 5;
assert_eq!(arr.find_me(find, 0, arr.len()-1), Some(12));
let mut arr = vec![3, 1, 4, 1, 5, 9, 2, 6, 5];
arr.quicksort();
assert_eq!(arr, [1, 1, 2, 3, 4, 5, 5, 6, 9]);