Skip to content
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

Unhandled Edge Case in Binary Search Implementation #1095

Open
ShadBalti opened this issue Dec 31, 2023 · 1 comment
Open

Unhandled Edge Case in Binary Search Implementation #1095

ShadBalti opened this issue Dec 31, 2023 · 1 comment

Comments

@ShadBalti
Copy link

The current implementation of the binary search algorithm in the JavaScript codebase does not handle a specific edge case where the target element is present multiple times in the sorted array. This issue is reported to address the potential misbehavior and propose a solution to handle scenarios where the binary search encounters duplicate elements.

Steps to Reproduce:

  1. Input a sorted array containing duplicate elements.
  2. Perform a binary search for a target element present multiple times in the array.
  3. Observe the behavior when the target element is found.

Expected Behavior:
The binary search algorithm should reliably locate and return any occurrence of the target element in the sorted array, providing accurate indices for all instances.

Current Behavior:
The current implementation may not consistently handle scenarios where duplicate elements exist in the sorted array, leading to unpredictable results when searching for the target element.

@faresh9
Copy link

faresh9 commented Jan 2, 2024

The claim is partially true. The current implementation of the binary search algorithm in the JavaScript code does not explicitly handle the case where the target element is present multiple times in the sorted array. The algorithm will find the index of the target element if it exists in the array, but it does not specify which occurrence of the element it will return.

In the code snippet:

if (comparator.equal(sortedArray[middleIndex], seekElement)) {
  return middleIndex;
}

If the target element is present multiple times, this condition will return the index of any occurrence of the element found during the search. It doesn't guarantee that it will return the first, last, or any specific occurrence of the element.

If handling duplicate elements in a specific way (e.g., returning the index of the first occurrence) is important for your use case, you might need to modify the binary search implementation accordingly. You could consider additional logic to determine how to handle duplicate elements based on your requirements.

So, while the claim is true in the sense that the provided implementation doesn't explicitly address the handling of duplicate elements, the behavior might still be acceptable depending on the context in which the binary search is used.

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

No branches or pull requests

2 participants