Skip to content
174 changes: 118 additions & 56 deletions src/searching/saddleback_search.rs
Original file line number Diff line number Diff line change
@@ -1,85 +1,147 @@
// Saddleback search is a technique used to find an element in a sorted 2D matrix in O(m + n) time,
// where m is the number of rows, and n is the number of columns. It works by starting from the
// top-right corner of the matrix and moving left or down based on the comparison of the current
// element with the target element.
use std::cmp::Ordering;

pub fn saddleback_search(matrix: &[Vec<i32>], element: i32) -> (usize, usize) {
// Initialize left and right indices
/// Performs Saddleback search on a sorted 2D matrix.
///
/// The Saddleback search algorithm finds the position of a target element in a matrix where
/// each row and each column is sorted in ascending order. The search starts from the top-right
/// corner of the matrix and moves left or down based on comparisons with the target element.
///
/// # Arguments
///
/// * `matrix` - A 2D vector representing the sorted matrix.
/// * `element` - The target element to search for.
///
/// # Returns
///
/// Returns `Some((row, column))` where both indices are 0-based. If the element is not found, returns `None`.
pub fn saddleback_search(matrix: &[Vec<isize>], element: isize) -> Option<(usize, usize)> {
let mut left_index = 0;
let mut right_index = matrix[0].len() - 1;

// Start searching
while left_index < matrix.len() {
match element.cmp(&matrix[left_index][right_index]) {
// If the current element matches the target element, return its position (indices are 1-based)
Ordering::Equal => return (left_index + 1, right_index + 1),
Ordering::Equal => return Some((left_index, right_index)),
Ordering::Greater => {
// If the target element is greater, move to the next row (downwards)
left_index += 1;
}
Ordering::Less => {
// If the target element is smaller, move to the previous column (leftwards)
if right_index == 0 {
break; // If we reach the left-most column, exit the loop
break;
} else {
right_index -= 1;
}
}
}
}

// If the element is not found, return (0, 0)
(0, 0)
None
}

#[cfg(test)]
mod tests {
use super::*;

// Test when the element is not present in the matrix
#[test]
fn test_element_not_found() {
let matrix = vec![vec![1, 10, 100], vec![2, 20, 200], vec![3, 30, 300]];
assert_eq!(saddleback_search(&matrix, 123), (0, 0));
}

// Test when the element is at the top-left corner of the matrix
#[test]
fn test_element_at_top_left() {
let matrix = vec![vec![1, 10, 100], vec![2, 20, 200], vec![3, 30, 300]];
assert_eq!(saddleback_search(&matrix, 1), (1, 1));
}

// Test when the element is at the bottom-right corner of the matrix
#[test]
fn test_element_at_bottom_right() {
let matrix = vec![vec![1, 10, 100], vec![2, 20, 200], vec![3, 30, 300]];
assert_eq!(saddleback_search(&matrix, 300), (3, 3));
}

// Test when the element is at the top-right corner of the matrix
#[test]
fn test_element_at_top_right() {
let matrix = vec![vec![1, 10, 100], vec![2, 20, 200], vec![3, 30, 300]];
assert_eq!(saddleback_search(&matrix, 100), (1, 3));
}

// Test when the element is at the bottom-left corner of the matrix
#[test]
fn test_element_at_bottom_left() {
let matrix = vec![vec![1, 10, 100], vec![2, 20, 200], vec![3, 30, 300]];
assert_eq!(saddleback_search(&matrix, 3), (3, 1));
macro_rules! saddleback_tests {
($($name:ident: $tc:expr,)*) => {
$(
#[test]
fn $name() {
let (matrix, element, expected) = $tc;
if let Some(expected_pos) = expected {
assert_eq!(matrix[expected_pos.0][expected_pos.1], element);
}
assert_eq!(saddleback_search(&matrix, element), expected);
}
)*
};
}

// Additional test case: Element in the middle of the matrix
#[test]
fn test_element_in_middle() {
let matrix = vec![
vec![1, 10, 100, 1000],
vec![2, 20, 200, 2000],
vec![3, 30, 300, 3000],
];
assert_eq!(saddleback_search(&matrix, 200), (2, 3));
saddleback_tests! {
test_element_not_found: (
vec![
vec![1, 10, 100],
vec![2, 20, 200],
vec![3, 30, 300]
],
123,
None::<(usize, usize)>,
),
test_element_at_top_left: (
vec![
vec![1, 10, 100],
vec![2, 20, 200],
vec![3, 30, 300]
],
1,
Some((0, 0)),
),
test_element_at_bottom_right: (
vec![
vec![1, 10, 100],
vec![2, 20, 200],
vec![3, 30, 300]
],
300,
Some((2, 2)),
),
test_element_at_top_right: (
vec![
vec![1, 10, 100],
vec![2, 20, 200],
vec![3, 30, 300]
],
100,
Some((0, 2)),
),
test_element_at_bottom_left: (
vec![
vec![1, 10, 100],
vec![2, 20, 200],
vec![3, 30, 300]
],
3,
Some((2, 0)),
),
test_element_in_middle: (
vec![
vec![1, 10, 100, 1000],
vec![2, 20, 200, 2000],
vec![3, 30, 300, 3000],
],
200,
Some((1, 2)),
),
test_element_smaller_than_min: (
vec![
vec![1, 10, 100],
vec![2, 20, 200],
vec![3, 30, 300],
],
0,
None::<(usize, usize)>,
),
test_horizontal: (
vec![
vec![1, 10, 100],
],
100,
Some((0, 2)),
),
test_vertical: (
vec![
vec![1],
vec![2],
vec![3],
],
2,
Some((1, 0)),
),
test_single_element: (
vec![
vec![1],
],
1,
Some((0, 0)),
),
}
}