Skip to content

Commit acc74d6

Browse files
committed
chore: return zero-based index
1 parent b54a02b commit acc74d6

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

src/searching/saddleback_search.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ use std::cmp::Ordering;
1313
///
1414
/// # Returns
1515
///
16-
/// Returns `Some((row, column))` where both indices are 1-based. If the element is not found, returns `None`.
16+
/// Returns `Some((row, column))` where both indices are 0-based. If the element is not found, returns `None`.
1717
pub fn saddleback_search(matrix: &[Vec<isize>], element: isize) -> Option<(usize, usize)> {
1818
let mut left_index = 0;
1919
let mut right_index = matrix[0].len() - 1;
2020

2121
while left_index < matrix.len() {
2222
match element.cmp(&matrix[left_index][right_index]) {
23-
Ordering::Equal => return Some((left_index + 1, right_index + 1)),
23+
Ordering::Equal => return Some((left_index, right_index)),
2424
Ordering::Greater => {
2525
left_index += 1;
2626
}
@@ -61,7 +61,7 @@ mod tests {
6161
vec![3, 30, 300]
6262
],
6363
123,
64-
None,
64+
None::<(usize, usize)>,
6565
),
6666
test_element_at_top_left: (
6767
vec![
@@ -70,7 +70,7 @@ mod tests {
7070
vec![3, 30, 300]
7171
],
7272
1,
73-
Some((1, 1)),
73+
Some((0, 0)),
7474
),
7575
test_element_at_bottom_right: (
7676
vec![
@@ -79,7 +79,7 @@ mod tests {
7979
vec![3, 30, 300]
8080
],
8181
300,
82-
Some((3, 3)),
82+
Some((2, 2)),
8383
),
8484
test_element_at_top_right: (
8585
vec![
@@ -88,7 +88,7 @@ mod tests {
8888
vec![3, 30, 300]
8989
],
9090
100,
91-
Some((1, 3)),
91+
Some((0, 2)),
9292
),
9393
test_element_at_bottom_left: (
9494
vec![
@@ -97,7 +97,7 @@ mod tests {
9797
vec![3, 30, 300]
9898
],
9999
3,
100-
Some((3, 1)),
100+
Some((2, 0)),
101101
),
102102
test_element_in_middle: (
103103
vec![
@@ -106,7 +106,7 @@ mod tests {
106106
vec![3, 30, 300, 3000],
107107
],
108108
200,
109-
Some((2, 3)),
109+
Some((1, 2)),
110110
),
111111
test_element_smaller_than_min: (
112112
vec![
@@ -115,7 +115,7 @@ mod tests {
115115
vec![3, 30, 300],
116116
],
117117
0,
118-
None,
118+
None::<(usize, usize)>,
119119
),
120120
}
121121
}

0 commit comments

Comments
 (0)