Skip to content

Commit 5afbec4

Browse files
sozelfistvil02
andauthored
Improve Linear Search (#826)
* ref: improve linear search * tests: verify test data --------- Co-authored-by: Piotr Idzik <[email protected]>
1 parent 114e01f commit 5afbec4

File tree

1 file changed

+59
-32
lines changed

1 file changed

+59
-32
lines changed

src/searching/linear_search.rs

+59-32
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
1-
use std::cmp::PartialEq;
2-
3-
pub fn linear_search<T: PartialEq>(item: &T, arr: &[T]) -> Option<usize> {
1+
/// Performs a linear search on the given array, returning the index of the first occurrence of the item.
2+
///
3+
/// # Arguments
4+
///
5+
/// * `item` - A reference to the item to search for in the array.
6+
/// * `arr` - A slice of items to search within.
7+
///
8+
/// # Returns
9+
///
10+
/// * `Some(usize)` - The index of the first occurrence of the item, if found.
11+
/// * `None` - If the item is not found in the array.
12+
pub fn linear_search<T: Ord>(item: &T, arr: &[T]) -> Option<usize> {
413
for (i, data) in arr.iter().enumerate() {
514
if item == data {
615
return Some(i);
@@ -14,36 +23,54 @@ pub fn linear_search<T: PartialEq>(item: &T, arr: &[T]) -> Option<usize> {
1423
mod tests {
1524
use super::*;
1625

17-
#[test]
18-
fn search_strings() {
19-
let index = linear_search(&"a", &["a", "b", "c", "d", "google", "zoo"]);
20-
assert_eq!(index, Some(0));
21-
}
22-
23-
#[test]
24-
fn search_ints() {
25-
let index = linear_search(&4, &[1, 2, 3, 4]);
26-
assert_eq!(index, Some(3));
27-
28-
let index = linear_search(&3, &[1, 2, 3, 4]);
29-
assert_eq!(index, Some(2));
30-
31-
let index = linear_search(&2, &[1, 2, 3, 4]);
32-
assert_eq!(index, Some(1));
33-
34-
let index = linear_search(&1, &[1, 2, 3, 4]);
35-
assert_eq!(index, Some(0));
36-
}
37-
38-
#[test]
39-
fn not_found() {
40-
let index = linear_search(&5, &[1, 2, 3, 4]);
41-
assert_eq!(index, None);
26+
macro_rules! test_cases {
27+
($($name:ident: $tc:expr,)*) => {
28+
$(
29+
#[test]
30+
fn $name() {
31+
let (item, arr, expected) = $tc;
32+
if let Some(expected_index) = expected {
33+
assert_eq!(arr[expected_index], item);
34+
}
35+
assert_eq!(linear_search(&item, arr), expected);
36+
}
37+
)*
38+
}
4239
}
4340

44-
#[test]
45-
fn empty() {
46-
let index = linear_search(&1, &[]);
47-
assert_eq!(index, None);
41+
test_cases! {
42+
empty: ("a", &[] as &[&str], None),
43+
one_item_found: ("a", &["a"], Some(0)),
44+
one_item_not_found: ("b", &["a"], None),
45+
search_strings_asc_start: ("a", &["a", "b", "c", "d", "google", "zoo"], Some(0)),
46+
search_strings_asc_middle: ("google", &["a", "b", "c", "d", "google", "zoo"], Some(4)),
47+
search_strings_asc_last: ("zoo", &["a", "b", "c", "d", "google", "zoo"], Some(5)),
48+
search_strings_asc_not_found: ("x", &["a", "b", "c", "d", "google", "zoo"], None),
49+
search_strings_desc_start: ("zoo", &["zoo", "google", "d", "c", "b", "a"], Some(0)),
50+
search_strings_desc_middle: ("google", &["zoo", "google", "d", "c", "b", "a"], Some(1)),
51+
search_strings_desc_last: ("a", &["zoo", "google", "d", "c", "b", "a"], Some(5)),
52+
search_strings_desc_not_found: ("x", &["zoo", "google", "d", "c", "b", "a"], None),
53+
search_ints_asc_start: (1, &[1, 2, 3, 4], Some(0)),
54+
search_ints_asc_middle: (3, &[1, 2, 3, 4], Some(2)),
55+
search_ints_asc_end: (4, &[1, 2, 3, 4], Some(3)),
56+
search_ints_asc_not_found: (5, &[1, 2, 3, 4], None),
57+
search_ints_desc_start: (4, &[4, 3, 2, 1], Some(0)),
58+
search_ints_desc_middle: (3, &[4, 3, 2, 1], Some(1)),
59+
search_ints_desc_end: (1, &[4, 3, 2, 1], Some(3)),
60+
search_ints_desc_not_found: (5, &[4, 3, 2, 1], None),
61+
with_gaps_0: (0, &[1, 3, 8, 11], None),
62+
with_gaps_1: (1, &[1, 3, 8, 11], Some(0)),
63+
with_gaps_2: (2, &[1, 3, 8, 11], None),
64+
with_gaps_3: (3, &[1, 3, 8, 11], Some(1)),
65+
with_gaps_4: (4, &[1, 3, 8, 10], None),
66+
with_gaps_5: (5, &[1, 3, 8, 10], None),
67+
with_gaps_6: (6, &[1, 3, 8, 10], None),
68+
with_gaps_7: (7, &[1, 3, 8, 11], None),
69+
with_gaps_8: (8, &[1, 3, 8, 11], Some(2)),
70+
with_gaps_9: (9, &[1, 3, 8, 11], None),
71+
with_gaps_10: (10, &[1, 3, 8, 11], None),
72+
with_gaps_11: (11, &[1, 3, 8, 11], Some(3)),
73+
with_gaps_12: (12, &[1, 3, 8, 11], None),
74+
with_gaps_13: (13, &[1, 3, 8, 11], None),
4875
}
4976
}

0 commit comments

Comments
 (0)