Skip to content

Commit bdaf9ef

Browse files
committed
ref: refactor ternary search
1 parent 0dbaff5 commit bdaf9ef

File tree

1 file changed

+159
-65
lines changed

1 file changed

+159
-65
lines changed

src/searching/ternary_search.rs

Lines changed: 159 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,91 +1,185 @@
1+
//! This module provides an implementation of a ternary search algorithm that
2+
//! works for both ascending and descending ordered arrays. The ternary search
3+
//! function returns the index of the target element if it is found, or `None`
4+
//! if the target is not present in the array.
5+
16
use std::cmp::Ordering;
27

3-
pub fn ternary_search<T: Ord>(
4-
target: &T,
5-
list: &[T],
6-
mut start: usize,
7-
mut end: usize,
8-
) -> Option<usize> {
9-
if list.is_empty() {
8+
/// Performs a ternary search for a specified item within a sorted array.
9+
///
10+
/// This function can handle both ascending and descending ordered arrays. It
11+
/// takes a reference to the item to search for and a slice of the array. If
12+
/// the item is found, it returns the index of the item within the array. If
13+
/// the item is not found, it returns `None`.
14+
///
15+
/// # Parameters
16+
///
17+
/// - `item`: A reference to the item to search for.
18+
/// - `arr`: A slice of the sorted array in which to search.
19+
///
20+
/// # Returns
21+
///
22+
/// An `Option<usize>` which is:
23+
/// - `Some(index)` if the item is found at the given index.
24+
/// - `None` if the item is not found in the array.
25+
pub fn ternary_search<T: Ord>(item: &T, arr: &[T]) -> Option<usize> {
26+
if arr.is_empty() {
1027
return None;
1128
}
1229

13-
while start <= end {
14-
let mid1: usize = start + (end - start) / 3;
15-
let mid2: usize = end - (end - start) / 3;
30+
let is_asc = is_asc_arr(arr);
31+
let mut left = 0;
32+
let mut right = arr.len() - 1;
1633

17-
match target.cmp(&list[mid1]) {
18-
Ordering::Less => end = mid1 - 1,
19-
Ordering::Equal => return Some(mid1),
20-
Ordering::Greater => match target.cmp(&list[mid2]) {
21-
Ordering::Greater => start = mid2 + 1,
22-
Ordering::Equal => return Some(mid2),
23-
Ordering::Less => {
24-
start = mid1 + 1;
25-
end = mid2 - 1;
26-
}
27-
},
34+
while left <= right {
35+
if match_compare(item, arr, &mut left, &mut right, is_asc) {
36+
return Some(left);
2837
}
2938
}
3039

3140
None
3241
}
3342

34-
#[cfg(test)]
35-
mod tests {
36-
use super::*;
43+
/// Compares the item with two middle elements of the current search range and
44+
/// updates the search bounds accordingly. This function handles both ascending
45+
/// and descending ordered arrays. It calculates two middle indices of the
46+
/// current search range and compares the item with the elements at these
47+
/// indices. It then updates the search bounds (`left` and `right`) based on
48+
/// the result of these comparisons. If the item is found, it returns `true`.
49+
///
50+
/// # Parameters
51+
///
52+
/// - `item`: A reference to the item to search for.
53+
/// - `arr`: A slice of the array in which to search.
54+
/// - `left`: A mutable reference to the left bound of the search range.
55+
/// - `right`: A mutable reference to the right bound of the search range.
56+
/// - `is_asc`: A boolean indicating whether the array is sorted in ascending order.
57+
///
58+
/// # Returns
59+
///
60+
/// A `bool` indicating:
61+
/// - `true` if the item was found in the array.
62+
/// - `false` if the item was not found in the array.
63+
fn match_compare<T: Ord>(
64+
item: &T,
65+
arr: &[T],
66+
left: &mut usize,
67+
right: &mut usize,
68+
is_asc: bool,
69+
) -> bool {
70+
let first_mid = *left + (*right - *left) / 3;
71+
let second_mid = *right - (*right - *left) / 3;
3772

38-
#[test]
39-
fn returns_none_if_empty_list() {
40-
let index = ternary_search(&"a", &[], 1, 10);
41-
assert_eq!(index, None);
42-
}
73+
// Handling the edge case where the search narrows down to a single element
74+
if first_mid == second_mid && first_mid == *left {
75+
if arr[*left] != *item {
76+
*left += 1;
77+
return false;
78+
}
4379

44-
#[test]
45-
fn returns_none_if_range_is_invalid() {
46-
let index = ternary_search(&1, &[1, 2, 3], 2, 1);
47-
assert_eq!(index, None);
80+
return true;
4881
}
4982

50-
#[test]
51-
fn returns_index_if_list_has_one_item() {
52-
let index = ternary_search(&1, &[1], 0, 1);
53-
assert_eq!(index, Some(0));
54-
}
83+
let cmp_first_mid = item.cmp(&arr[first_mid]);
84+
let cmp_second_mid = item.cmp(&arr[second_mid]);
5585

56-
#[test]
57-
fn returns_first_index() {
58-
let index = ternary_search(&1, &[1, 2, 3], 0, 2);
59-
assert_eq!(index, Some(0));
86+
match (is_asc, cmp_first_mid, cmp_second_mid) {
87+
(_, Ordering::Equal, _) => {
88+
*left = first_mid;
89+
return true;
90+
}
91+
(_, _, Ordering::Equal) => {
92+
*left = second_mid;
93+
return true;
94+
}
95+
(true, Ordering::Less, _) | (false, Ordering::Greater, _) => {
96+
*right = first_mid.saturating_sub(1)
97+
}
98+
(true, _, Ordering::Greater) | (false, _, Ordering::Less) => *left = second_mid + 1,
99+
(_, _, _) => {
100+
*left = first_mid + 1;
101+
*right = second_mid - 1;
102+
}
60103
}
61104

62-
#[test]
63-
fn returns_first_index_if_end_out_of_bounds() {
64-
let index = ternary_search(&1, &[1, 2, 3], 0, 3);
65-
assert_eq!(index, Some(0));
66-
}
105+
false
106+
}
67107

68-
#[test]
69-
fn returns_last_index() {
70-
let index = ternary_search(&3, &[1, 2, 3], 0, 2);
71-
assert_eq!(index, Some(2));
72-
}
108+
/// Determines if the given array is sorted in ascending order.
109+
///
110+
/// This helper function checks if the first element of the array is less than the
111+
/// last element, indicating an ascending order. It returns `false` if the array
112+
/// has fewer than two elements.
113+
///
114+
/// # Parameters
115+
///
116+
/// - `arr`: A slice of the array to check.
117+
///
118+
/// # Returns
119+
///
120+
/// A `bool` indicating whether the array is sorted in ascending order.
121+
fn is_asc_arr<T: Ord>(arr: &[T]) -> bool {
122+
arr.len() > 1 && arr[0] < arr[arr.len() - 1]
123+
}
73124

74-
#[test]
75-
fn returns_last_index_if_end_out_of_bounds() {
76-
let index = ternary_search(&3, &[1, 2, 3], 0, 3);
77-
assert_eq!(index, Some(2));
78-
}
125+
#[cfg(test)]
126+
mod tests {
127+
use super::*;
79128

80-
#[test]
81-
fn returns_middle_index() {
82-
let index = ternary_search(&2, &[1, 2, 3], 0, 2);
83-
assert_eq!(index, Some(1));
129+
macro_rules! test_cases {
130+
($($name:ident: $test_case:expr,)*) => {
131+
$(
132+
#[test]
133+
fn $name() {
134+
let (item, arr, expected) = $test_case;
135+
assert_eq!(ternary_search(&item, arr), expected);
136+
}
137+
)*
138+
};
84139
}
85140

86-
#[test]
87-
fn returns_middle_index_if_end_out_of_bounds() {
88-
let index = ternary_search(&2, &[1, 2, 3], 0, 3);
89-
assert_eq!(index, Some(1));
141+
test_cases! {
142+
empty: ("a", &[] as &[&str], None),
143+
one_item_found: ("a", &["a"], Some(0)),
144+
one_item_not_found: ("b", &["a"], None),
145+
search_two_elements_found_at_start: (1, &[1, 2], Some(0)),
146+
search_two_elements_found_at_end: (2, &[1, 2], Some(1)),
147+
search_two_elements_not_found_start: (0, &[1, 2], None),
148+
search_two_elements_not_found_end: (3, &[1, 2], None),
149+
search_three_elements_found_start: (1, &[1, 2, 3], Some(0)),
150+
search_three_elements_found_middle: (2, &[1, 2, 3], Some(1)),
151+
search_three_elements_found_end: (3, &[1, 2, 3], Some(2)),
152+
search_three_elements_not_found_start: (0, &[1, 2, 3], None),
153+
search_three_elements_not_found_end: (4, &[1, 2, 3], None),
154+
search_strings_asc_start: ("a", &["a", "b", "c", "d", "google", "zoo"], Some(0)),
155+
search_strings_asc_middle: ("google", &["a", "b", "c", "d", "google", "zoo"], Some(4)),
156+
search_strings_asc_last: ("zoo", &["a", "b", "c", "d", "google", "zoo"], Some(5)),
157+
search_strings_asc_not_found: ("x", &["a", "b", "c", "d", "google", "zoo"], None),
158+
search_strings_desc_start: ("zoo", &["zoo", "google", "d", "c", "b", "a"], Some(0)),
159+
search_strings_desc_middle: ("google", &["zoo", "google", "d", "c", "b", "a"], Some(1)),
160+
search_strings_desc_last: ("a", &["zoo", "google", "d", "c", "b", "a"], Some(5)),
161+
search_strings_desc_not_found: ("x", &["zoo", "google", "d", "c", "b", "a"], None),
162+
search_ints_asc_start: (1, &[1, 2, 3, 4], Some(0)),
163+
search_ints_asc_middle: (3, &[1, 2, 3, 4], Some(2)),
164+
search_ints_asc_end: (4, &[1, 2, 3, 4], Some(3)),
165+
search_ints_asc_not_found: (5, &[1, 2, 3, 4], None),
166+
search_ints_desc_start: (4, &[4, 3, 2, 1], Some(0)),
167+
search_ints_desc_middle: (3, &[4, 3, 2, 1], Some(1)),
168+
search_ints_desc_end: (1, &[4, 3, 2, 1], Some(3)),
169+
search_ints_desc_not_found: (5, &[4, 3, 2, 1], None),
170+
with_gaps_0: (0, &[1, 3, 8, 11], None),
171+
with_gaps_1: (1, &[1, 3, 8, 11], Some(0)),
172+
with_gaps_2: (2, &[1, 3, 8, 11], None),
173+
with_gaps_3: (3, &[1, 3, 8, 11], Some(1)),
174+
with_gaps_4: (4, &[1, 3, 8, 10], None),
175+
with_gaps_5: (5, &[1, 3, 8, 10], None),
176+
with_gaps_6: (6, &[1, 3, 8, 10], None),
177+
with_gaps_7: (7, &[1, 3, 8, 11], None),
178+
with_gaps_8: (8, &[1, 3, 8, 11], Some(2)),
179+
with_gaps_9: (9, &[1, 3, 8, 11], None),
180+
with_gaps_10: (10, &[1, 3, 8, 11], None),
181+
with_gaps_11: (11, &[1, 3, 8, 11], Some(3)),
182+
with_gaps_12: (12, &[1, 3, 8, 11], None),
183+
with_gaps_13: (13, &[1, 3, 8, 11], None),
90184
}
91185
}

0 commit comments

Comments
 (0)