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.


翻译:二分查找虽在概念上看似简单,但实现正确却以困难著称。本文对二分查找进行了统一论述:五种核心变体、六种派生查询函数,以及四种标准库实现(BSD、glibc、Java、C++ STL),每种均配以一致的符号表示、循环不变量及分析。我们引入bsearch_ultimate——一种通过单一调用涵盖所有变体的组合式搜索方法。每种算法均提供同步Python代码、Dafny形式化证明及伪代码。所有实现经由超过9500项测试及21次Dafny形式化验证;额外六种人为植入缺陷的实现则展示了常见错误类别及Dafny检测这些错误的能力。我们还提供了将边界选择与循环条件及更新公式相关联的易于记忆的规则。

0
下载
关闭预览

相关内容

【NeurIPS2021】SOLQ:基于学习查询的物体分割
专知会员服务
10+阅读 · 2021年11月9日
一种关键字提取新方法
1号机器人网
21+阅读 · 2018年11月15日
【论文】变分推断(Variational inference)的总结
机器学习研究会
39+阅读 · 2017年11月16日
国家自然科学基金
8+阅读 · 2015年12月31日
国家自然科学基金
3+阅读 · 2015年12月31日
国家自然科学基金
1+阅读 · 2015年12月31日
国家自然科学基金
0+阅读 · 2015年12月31日
国家自然科学基金
0+阅读 · 2015年12月31日
国家自然科学基金
3+阅读 · 2015年12月31日
国家自然科学基金
2+阅读 · 2014年12月31日
国家自然科学基金
1+阅读 · 2014年12月31日
国家自然科学基金
0+阅读 · 2014年12月31日
国家自然科学基金
2+阅读 · 2014年12月31日
Arxiv
0+阅读 · 6月14日
VIP会员
最新内容
人工智能在军事教育中的应用:若干视角与经验
“星链”减速与不断适应的乌克兰
专知会员服务
1+阅读 · 11月6日
美国中期选举之后:帕兰提尔面临不确定性?
专知会员服务
2+阅读 · 11月6日
《面向国防领域的大语言模型基准测试与适配》
专知会员服务
17+阅读 · 10月28日
世界模型入门:从序贯决策到通用智能体
专知会员服务
19+阅读 · 10月24日
相关VIP内容
【NeurIPS2021】SOLQ:基于学习查询的物体分割
专知会员服务
10+阅读 · 2021年11月9日
相关基金
国家自然科学基金
8+阅读 · 2015年12月31日
国家自然科学基金
3+阅读 · 2015年12月31日
国家自然科学基金
1+阅读 · 2015年12月31日
国家自然科学基金
0+阅读 · 2015年12月31日
国家自然科学基金
0+阅读 · 2015年12月31日
国家自然科学基金
3+阅读 · 2015年12月31日
国家自然科学基金
2+阅读 · 2014年12月31日
国家自然科学基金
1+阅读 · 2014年12月31日
国家自然科学基金
0+阅读 · 2014年12月31日
国家自然科学基金
2+阅读 · 2014年12月31日
Top
微信扫码咨询专知VIP会员