二分搜索变体:全面分析
Binary Search Variants: A Comprehensive Analysis
AI总结:
本文统一处理二分搜索的五种核心变体、六种派生查询函数和四种标准库实现,引入bsearch_ultimate组合搜索,并通过Python代码、Dafny形式化证明和伪代码提供所有算法,经9500余次测试和21次Dafny验证。
摘要:
二分搜索概念上看似简单,但正确实现却出了名地困难。本文对二分搜索进行了统一处理:五种核心变体、六种派生查询函数以及四种标准库实现(BSD、glibc、Java、C++ STL),每种都附带一致的符号表示、循环不变量和分析。我们引入了bsearch_ultimate,一种组合搜索,可在单次调用中涵盖所有变体。每个算法都以同步的Python代码、Dafny形式化证明和伪代码形式提供。所有实现均经过超过9500次测试和21次Dafny形式化验证;另外六个故意有缺陷的实现展示了常见的错误类别以及Dafny检测它们的能力。我们还提供了易于记忆的规则,将边界选择与循环条件和更新公式联系起来。
英文摘要:
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.
机构、总结、中文摘要均由AI分析而来,可能存在错误,请以论文正文为准。