From 5afbec4427a1164fed539363caf7db0197ba84e1 Mon Sep 17 00:00:00 2001 From: Truong Nhan Nguyen <80200848+sozelfist@users.noreply.github.com> Date: Fri, 25 Oct 2024 23:18:12 +0700 Subject: [PATCH] Improve Linear Search (#826) * ref: improve linear search * tests: verify test data --------- Co-authored-by: Piotr Idzik <65706193+vil02@users.noreply.github.com> --- src/searching/linear_search.rs | 91 ++++++++++++++++++++++------------ 1 file changed, 59 insertions(+), 32 deletions(-) diff --git a/src/searching/linear_search.rs b/src/searching/linear_search.rs index c2995754509..d38b224d0a6 100644 --- a/src/searching/linear_search.rs +++ b/src/searching/linear_search.rs @@ -1,6 +1,15 @@ -use std::cmp::PartialEq; - -pub fn linear_search(item: &T, arr: &[T]) -> Option { +/// 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(item: &T, arr: &[T]) -> Option { for (i, data) in arr.iter().enumerate() { if item == data { return Some(i); @@ -14,36 +23,54 @@ pub fn linear_search(item: &T, arr: &[T]) -> Option { 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; + if let Some(expected_index) = expected { + assert_eq!(arr[expected_index], item); + } + 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), } }