Skip to content

Conversation

@GziXnine
Copy link
Contributor

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized it.
  • All filenames are in PascalCase.
  • All functions and variable names follow Java naming conventions.
  • All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.
  • All new algorithms include a corresponding test class that validates their functionality.
  • All new code is formatted with clang-format -i --style=file path/to/your/file.java

Summary

This PR adds two reusable 2D matrix search utilities under com.thealgorithms.matrix.

  • SearchSortedMatrix implements Saddleback (“staircase”) search for matrices whose rows and columns are sorted in ascending order, providing an efficient O(m+n) membership check without flattening.
  • SearchMatrix provides a general-purpose contains method for arbitrary T[][] matrices with no ordering assumptions, using a straightforward linear scan.

Details

  • SearchSortedMatrix

    • Safe handling for null/empty matrices.
    • Validates rectangular (non-jagged) shape and rejects null rows to avoid undefined behavior when the sorted-matrix assumptions are violated.
    • Time O(m+n), space O(1).
  • SearchMatrix

    • Works with any object matrix type T[][].
    • Null-safe and supports jagged matrices and null elements via Objects.equals.
    • Time O(m*n), space O(1).

Tests

Adds unit coverage for both algorithms:

  • SearchSortedMatrixTest
  • SearchMatrixTest

These tests cover typical found/not-found cases and edge conditions (null/empty input, jagged matrices, and null rows/elements where applicable).

Closes #6622

@codecov-commenter
Copy link

codecov-commenter commented Jan 17, 2026

Codecov Report

❌ Patch coverage is 98.18182% with 1 line in your changes missing coverage. Please review.
⚠️ Please upload report for BASE (master@fd0bcb7). Learn more about missing BASE report.

Files with missing lines Patch % Lines
...a/com/thealgorithms/matrix/SearchSortedMatrix.java 97.82% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff            @@
##             master    #7217   +/-   ##
=========================================
  Coverage          ?   79.11%           
  Complexity        ?     6976           
=========================================
  Files             ?      781           
  Lines             ?    22961           
  Branches          ?     4520           
=========================================
  Hits              ?    18165           
  Misses            ?     4076           
  Partials          ?      720           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[FEATURE REQUEST] : Add Search a 2D Matrix in src/main/java/com/thealgorithms/matrix.

2 participants