-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Cleanup Saddleback Search #824
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
sozelfist
wants to merge
12
commits into
TheAlgorithms:master
from
sozelfist:ref/search/saddleback_search
Closed
Changes from 3 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
b8fc4fe
ref: improve saddleback search
sozelfist 4c31581
tests: add `test_element_smaller_than_min`
sozelfist 326bded
Merge branch 'master' into ref/search/saddleback_search
sozelfist b54a02b
chore: return `Option<(usize, usize)>`
sozelfist acc74d6
chore: return zero-based index
sozelfist 95aa66e
tests: verify test data
vil02 f5f6179
tests: add some edge cases
vil02 78bd242
ref: refactor implementation
sozelfist 1fb9023
ref: let saddleback search check if the input is sorted
sozelfist 4436d2c
test: add some edge tests
sozelfist 9b15298
ref: remove unneed check for empty matrix
sozelfist 928c630
Merge branch 'master' into ref/search/saddleback_search
sozelfist File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,85 +1,121 @@ | ||
| // 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. | ||
sozelfist marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| /// | ||
| /// 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. | ||
sozelfist marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| /// * `element` - The target element to search for. | ||
| /// | ||
| /// # Returns | ||
| /// | ||
| /// Returns a tuple (row, column) where both indices are 1-based. If the element is not found, returns (0, 0). | ||
vil02 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| pub fn saddleback_search(matrix: &[Vec<isize>], element: isize) -> (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::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) | ||
| } | ||
|
|
||
| #[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; | ||
| 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, | ||
| (0, 0), | ||
| ), | ||
| test_element_at_top_left: ( | ||
| vec![ | ||
| vec![1, 10, 100], | ||
| vec![2, 20, 200], | ||
| vec![3, 30, 300] | ||
| ], | ||
| 1, | ||
| (1, 1), | ||
| ), | ||
| test_element_at_bottom_right: ( | ||
| vec![ | ||
| vec![1, 10, 100], | ||
| vec![2, 20, 200], | ||
| vec![3, 30, 300] | ||
| ], | ||
| 300, | ||
| (3, 3), | ||
| ), | ||
| test_element_at_top_right: ( | ||
| vec![ | ||
| vec![1, 10, 100], | ||
| vec![2, 20, 200], | ||
| vec![3, 30, 300] | ||
| ], | ||
| 100, | ||
| (1, 3), | ||
| ), | ||
| test_element_at_bottom_left: ( | ||
| vec![ | ||
| vec![1, 10, 100], | ||
| vec![2, 20, 200], | ||
| vec![3, 30, 300] | ||
| ], | ||
| 3, | ||
| (3, 1), | ||
| ), | ||
| test_element_in_middle: ( | ||
| vec![ | ||
| vec![1, 10, 100, 1000], | ||
| vec![2, 20, 200, 2000], | ||
| vec![3, 30, 300, 3000], | ||
| ], | ||
| 200, | ||
| (2, 3), | ||
| ), | ||
| test_element_smaller_than_min: ( | ||
| vec![ | ||
| vec![1, 10, 100], | ||
| vec![2, 20, 200], | ||
| vec![3, 30, 300], | ||
| ], | ||
| 0, | ||
| (0, 0), | ||
| ), | ||
sozelfist marked this conversation as resolved.
Show resolved
Hide resolved
sozelfist marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.