added bubble_sort, binary_search, linear_search
This commit is contained in:
commit
f895efce44
13 changed files with 305 additions and 0 deletions
63
src/binary_search.rs
Normal file
63
src/binary_search.rs
Normal file
|
|
@ -0,0 +1,63 @@
|
|||
#[cfg (test)]
|
||||
mod tests {
|
||||
use super::binary_search;
|
||||
#[test]
|
||||
fn test_binary_search_found() {
|
||||
let haystack = [1, 3, 5, 7, 9];
|
||||
assert_eq!(binary_search(&haystack, 5), Some(true));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_binary_search_not_found() {
|
||||
let haystack = [1, 3, 5, 7, 9];
|
||||
assert_eq!(binary_search(&haystack, 4), Some(false));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_binary_search_empty() {
|
||||
let haystack: [usize; 0] = [];
|
||||
assert_eq!(binary_search(&haystack, 4), Some(false));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_binary_search_single_element_found() {
|
||||
let haystack = [5];
|
||||
assert_eq!(binary_search(&haystack, 5), Some(true));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_binary_search_single_element_not_found() {
|
||||
let haystack = [5];
|
||||
assert_eq!(binary_search(&haystack, 3), Some(false));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_binary_search_multiple_elements_not_found() {
|
||||
let haystack = [1, 2, 3, 4, 5];
|
||||
assert_eq!(binary_search(&haystack, 6), Some(false));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_binary_search_multiple_elements_found() {
|
||||
let haystack = [1, 2, 3, 4, 5];
|
||||
assert_eq!(binary_search(&haystack, 3), Some(true));
|
||||
}
|
||||
}
|
||||
fn binary_search(haystack: &[usize], needle: usize) -> Option<bool> {
|
||||
let mut low = 0;
|
||||
let mut high = haystack.len();
|
||||
|
||||
while low < high {
|
||||
let midpoint = low + (high - low) / 2;
|
||||
let value = haystack[midpoint];
|
||||
if value == needle {
|
||||
return Some(true);
|
||||
} else if value > needle {
|
||||
high = midpoint;
|
||||
} else {
|
||||
low = midpoint + 1;
|
||||
}
|
||||
}
|
||||
|
||||
Some(false)
|
||||
}
|
||||
51
src/bubble_sort.rs
Normal file
51
src/bubble_sort.rs
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::bubble_sort;
|
||||
#[test]
|
||||
fn bubble_sort_already_sorted_input(){
|
||||
let mut arr = [1, 2, 3, 4, 5];
|
||||
bubble_sort(&mut arr);
|
||||
assert_eq!(arr, [1, 2, 3, 4, 5]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bubble_sort_reverse_sorted_input(){
|
||||
let mut arr = [5, 4, 3, 2, 1];
|
||||
bubble_sort(&mut arr);
|
||||
assert_eq!(arr, [1, 2, 3, 4, 5]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bubble_sort_input_w_duplicate_elems(){
|
||||
let mut arr = [3, 1, 4, 1, 5, 9, 2, 6, 5];
|
||||
bubble_sort(&mut arr);
|
||||
assert_eq!(arr, [1, 1, 2, 3, 4, 5, 5, 6, 9]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bubble_sort_empty_input(){
|
||||
let mut arr: [usize; 0] = [];
|
||||
bubble_sort(&mut arr);
|
||||
assert_eq!(arr, []);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bubble_sort_input_w_one_elem(){
|
||||
let mut arr = [42];
|
||||
bubble_sort(&mut arr);
|
||||
assert_eq!(arr, [42]);
|
||||
}
|
||||
}
|
||||
fn bubble_sort(arr: &mut [usize]) {
|
||||
let n = arr.len();
|
||||
for i in 0..n {
|
||||
for j in 0..n - 1 - i {
|
||||
if arr[j] > arr[j + 1] {
|
||||
let tmp = arr[j];
|
||||
arr[j] = arr[j + 1];
|
||||
arr[j + 1] = tmp;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
54
src/linear_search.rs
Normal file
54
src/linear_search.rs
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::linear_search;
|
||||
|
||||
#[test]
|
||||
fn test_linear_search_found() {
|
||||
let haystack = [1, 3, 5, 7, 9];
|
||||
assert_eq!(linear_search(&haystack, 5), Some((true, 2)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_linear_search_not_found() {
|
||||
let haystack = [1, 3, 5, 7, 9];
|
||||
assert_eq!(linear_search(&haystack, 4), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_linear_search_empty() {
|
||||
let haystack: [usize; 0] = [];
|
||||
assert_eq!(linear_search(&haystack, 4), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_linear_search_single_element_found() {
|
||||
let haystack = [5];
|
||||
assert_eq!(linear_search(&haystack, 5), Some((true, 0)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_linear_search_single_element_not_found() {
|
||||
let haystack = [5];
|
||||
assert_eq!(linear_search(&haystack, 3), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_linear_search_multiple_elements_not_found() {
|
||||
let haystack = [1, 2, 3, 4, 5];
|
||||
assert_eq!(linear_search(&haystack, 6), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_linear_search_multiple_elements_found() {
|
||||
let haystack = [1, 2, 3, 4, 5];
|
||||
assert_eq!(linear_search(&haystack, 3), Some((true, 2)));
|
||||
}
|
||||
}
|
||||
fn linear_search(haystack: &[usize], needle: usize) -> Option<(bool, usize)> {
|
||||
for (idx, &n) in haystack.iter().enumerate() {
|
||||
if n == needle {
|
||||
return Some((true, idx));
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
5
src/main.rs
Normal file
5
src/main.rs
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
mod linear_search;
|
||||
mod binary_search;
|
||||
mod bubble_sort;
|
||||
fn main() {
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue