Binary Search Variants: A Comprehensive Analysis

📅 2026-06-11
📈 Citations: 0
Influential: 0
📄 PDF
🤖 AI Summary
Although binary search is conceptually simple, its implementation is notoriously error-prone and lacks a unified, reliable methodology. This work proposes the first comprehensive framework encompassing five core variants and six derived query types, introducing a universal algorithm, *bsearch_ultimate*. The framework establishes a symbolic system grounded in loop invariants, along with mnemonic rules for boundary selection, loop conditions, and update formulas. Correctness and robustness are rigorously demonstrated through three complementary representations—Python implementation, Dafny formal verification, and pseudocode—supported by over 9,500 test cases and 21 formal proofs. This systematic approach identifies six common classes of implementation errors, offering a principled and verifiably correct foundation for binary search across diverse scenarios.
📝 Abstract
Binary search is deceptively simple in concept yet notoriously difficult to implement correctly. This paper presents a unified treatment of binary search: five core variants, six derived query functions, and four standard library implementations (BSD, glibc, Java, C++ STL), each with consistent notation, loop invariants, and analysis. We introduce bsearch_ultimate, a combined search that subsumes all variants in a single call. Every algorithm is provided as synchronized Python code, Dafny formal proof, and pseudocode. All implementations are validated by over 9,500 tests and 21 Dafny formal verifications; an additional six deliberately faulty implementations demonstrate common bug categories and Dafny's ability to detect them. We also provide memorable rules linking boundary choices to loop conditions and update formulas.
Problem

Research questions and friction points this paper is trying to address.

binary search
implementation correctness
algorithm variants
formal verification
loop invariants
Innovation

Methods, ideas, or system contributions that make the work stand out.

binary search variants
formal verification
bsearch_ultimate
loop invariants
Dafny