Skip to content

Commit

Permalink
ref: improve linear search
Browse files Browse the repository at this point in the history
  • Loading branch information
sozelfist committed Oct 18, 2024
1 parent 274ca13 commit 4850aeb
Showing 1 changed file with 56 additions and 32 deletions.
88 changes: 56 additions & 32 deletions src/searching/linear_search.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
use std::cmp::PartialEq;

pub fn linear_search<T: PartialEq>(item: &T, arr: &[T]) -> Option<usize> {
/// Performs a linear search on the given array, returning the index of the first occurrence of the item.
///
/// # Arguments
///
/// * `item` - A reference to the item to search for in the array.
/// * `arr` - A slice of items to search within.
///
/// # Returns
///
/// * `Some(usize)` - The index of the first occurrence of the item, if found.
/// * `None` - If the item is not found in the array.
pub fn linear_search<T: Ord>(item: &T, arr: &[T]) -> Option<usize> {
for (i, data) in arr.iter().enumerate() {
if item == data {
return Some(i);
Expand All @@ -14,36 +23,51 @@ pub fn linear_search<T: PartialEq>(item: &T, arr: &[T]) -> Option<usize> {
mod tests {
use super::*;

#[test]
fn search_strings() {
let index = linear_search(&"a", &["a", "b", "c", "d", "google", "zoo"]);
assert_eq!(index, Some(0));
}

#[test]
fn search_ints() {
let index = linear_search(&4, &[1, 2, 3, 4]);
assert_eq!(index, Some(3));

let index = linear_search(&3, &[1, 2, 3, 4]);
assert_eq!(index, Some(2));

let index = linear_search(&2, &[1, 2, 3, 4]);
assert_eq!(index, Some(1));

let index = linear_search(&1, &[1, 2, 3, 4]);
assert_eq!(index, Some(0));
}

#[test]
fn not_found() {
let index = linear_search(&5, &[1, 2, 3, 4]);
assert_eq!(index, None);
macro_rules! test_cases {
($($name:ident: $tc:expr,)*) => {
$(
#[test]
fn $name() {
let (item, arr, expected) = $tc;
assert_eq!(linear_search(&item, arr), expected);
}
)*
}
}

#[test]
fn empty() {
let index = linear_search(&1, &[]);
assert_eq!(index, None);
test_cases! {
empty: ("a", &[] as &[&str], None),
one_item_found: ("a", &["a"], Some(0)),
one_item_not_found: ("b", &["a"], None),
search_strings_asc_start: ("a", &["a", "b", "c", "d", "google", "zoo"], Some(0)),
search_strings_asc_middle: ("google", &["a", "b", "c", "d", "google", "zoo"], Some(4)),
search_strings_asc_last: ("zoo", &["a", "b", "c", "d", "google", "zoo"], Some(5)),
search_strings_asc_not_found: ("x", &["a", "b", "c", "d", "google", "zoo"], None),
search_strings_desc_start: ("zoo", &["zoo", "google", "d", "c", "b", "a"], Some(0)),
search_strings_desc_middle: ("google", &["zoo", "google", "d", "c", "b", "a"], Some(1)),
search_strings_desc_last: ("a", &["zoo", "google", "d", "c", "b", "a"], Some(5)),
search_strings_desc_not_found: ("x", &["zoo", "google", "d", "c", "b", "a"], None),
search_ints_asc_start: (1, &[1, 2, 3, 4], Some(0)),
search_ints_asc_middle: (3, &[1, 2, 3, 4], Some(2)),
search_ints_asc_end: (4, &[1, 2, 3, 4], Some(3)),
search_ints_asc_not_found: (5, &[1, 2, 3, 4], None),
search_ints_desc_start: (4, &[4, 3, 2, 1], Some(0)),
search_ints_desc_middle: (3, &[4, 3, 2, 1], Some(1)),
search_ints_desc_end: (1, &[4, 3, 2, 1], Some(3)),
search_ints_desc_not_found: (5, &[4, 3, 2, 1], None),
with_gaps_0: (0, &[1, 3, 8, 11], None),
with_gaps_1: (1, &[1, 3, 8, 11], Some(0)),
with_gaps_2: (2, &[1, 3, 8, 11], None),
with_gaps_3: (3, &[1, 3, 8, 11], Some(1)),
with_gaps_4: (4, &[1, 3, 8, 10], None),
with_gaps_5: (5, &[1, 3, 8, 10], None),
with_gaps_6: (6, &[1, 3, 8, 10], None),
with_gaps_7: (7, &[1, 3, 8, 11], None),
with_gaps_8: (8, &[1, 3, 8, 11], Some(2)),
with_gaps_9: (9, &[1, 3, 8, 11], None),
with_gaps_10: (10, &[1, 3, 8, 11], None),
with_gaps_11: (11, &[1, 3, 8, 11], Some(3)),
with_gaps_12: (12, &[1, 3, 8, 11], None),
with_gaps_13: (13, &[1, 3, 8, 11], None),
}
}

0 comments on commit 4850aeb

Please sign in to comment.