镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

Remove inefficient validation checks in binary search functions - #13836

Closed
MathewAddala wants to merge 2 commits into
TheAlgorithms:masterfrom
MathewAddala:patch-3
Closed

MathewAddala wants to merge 2 commits into
TheAlgorithms:masterfrom
MathewAddala:patch-3

Conversation

@MathewAddala

Copy link
Copy Markdown
Contributor

Description

This PR fixes a severe performance defect in the binary search implementations by removing inefficient validation checks that defeat the purpose of using binary search.

Problem

The validation check if list(sorted_collection) != sorted(sorted_collection) was present in four functions:

  • binary_search
  • binary_search_std_lib
  • binary_search_by_recursion
  • exponential_search

This validation creates a full list copy of the collection on every function call, which has O(n) time complexity. This completely defeats the purpose of binary search algorithms, which should be O(log n). For large collections, this validation alone becomes more expensive than the search operation itself.

Solution

Removed the validation checks from all four functions. The functions already clearly document in their docstrings that the collection must be sorted in ascending order. This is a documented precondition that callers must satisfy, not something to validate at runtime in performance-critical code.

Impact

  • Performance: Binary search operations will now run in O(log n) time as intended, instead of O(n)
  • Behavior: Functions still work correctly when given properly sorted input (as documented)
  • Breaking Change: None - functions still behave the same for valid inputs

Describe your change:

  • Fix a bug or typo in an existing algorithm? ✅ (Performance bug)

Checklist:

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file.
  • All functions have doctests that pass the automated testing.

MathewAddala and others added 2 commits November 1, 2025 14:45
This commit removes the inefficient validation check `if list(sorted_collection) != sorted(sorted_collection)` from four functions:
- binary_search
- binary_search_std_lib
- binary_search_by_recursion
- exponential_search

This validation creates a full list copy of the collection on every call, which has O(n) time complexity. This defeats the purpose of binary search algorithms which should be O(log n). The validation also causes significant performance degradation, especially for large collections.

The functions already document in their docstrings that the collection must be sorted. This is a precondition, not something to validate at runtime in performance-critical code.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants