我仍然相信 OP 犯了 1 个错误。
我强烈怀疑
findMatchesInDict(word, start, dict, middle - 1, results, totalResults);
应该是
findMatchesInDict(word, start, dict, middle, results, totalResults);
我制作了自己的小样本。 (因此,我重新设计了代码,因为我对 OP 的做法感到不走运。)
#include <iostream>
#include <string>
size_t find(const std::string &word, const std::string dict[], size_t i0, size_t size)
{
if (!size) return (size_t)-1; // bail out with invalid index
const size_t i = i0 + size / 2;
return word == dict[i]
? i
: word < dict[i]
? find(word, dict, i0, i - i0)
: find(word, dict, i + 1, i0 + size - (i + 1));
}
int main()
{
const std::string dict[] = {
"Ada", "BASIC", "C", "C++",
"D", "Haskell", "INTERCAL", "Modula2",
"Oberon", "Pascal", "Scala", "Scratch",
"Vala"
};
const size_t sizeDict = sizeof dict / sizeof *dict;
unsigned nErrors = 0;
// brute force tests to find something what is in
for (size_t n = 1; n <= sizeDict; ++n) {
for (size_t i = 0; i < n; ++i) {
if (find(dict[i], dict, 0, n) >= n) {
std::cerr << "ALERT! Unable to find entry " << i << " in " << n << " entries!\n";
++nErrors;
}
}
}
// brute force tests to find something what is not in
for (size_t n = 1; n <= sizeDict; ++n) {
if (find("", dict, 0, n) < n) {
std::cerr << "ALERT! Able to find entry '' in " << n << " entries!\n";
++nErrors;
}
for (size_t i = 0; i < n; ++i) {
if (find(dict[i] + " + Assembler", dict, 0, n) < n) {
std::cerr << "ALERT! Able to find entry '" << dict[i] << " + Assembler' in " << n << " entries!\n";
++nErrors;
}
}
}
// report
if (!nErrors) std::cout << "All tests passed OK.\n";
else std::cerr << nErrors << " tests failed!\n";
// done
return nErrors > 0;
}
Live Demo on coliru
这段代码大部分是暴力测试代码:
测试从 1 到大小为 dict 的每个长度。对于每个长度,搜索dict 的任何条目。
测试从 1 到大小为 dict 的每个长度。对于每个长度,都会测试空字符串(在任何其他条目之前)以及任何带有修改的条目。 (修改允许它将在未修改条目与其后继条目之间或在最后一个条目之后。)
输出:
All tests passed OK.
一切顺利。
然后我换了
find(word, dict, i0, i - i0)
与
find(word, dict, i0, i - i0 > 0 ? i - i0 - 1 : 0)
类似于(在我看来)OP 的代码有什么问题。
输出:
ALERT! Unable to find entry 0 in 2 entries!
ALERT! Unable to find entry 0 in 3 entries!
ALERT! Unable to find entry 1 in 4 entries!
ALERT! Unable to find entry 1 in 5 entries!
ALERT! Unable to find entry 3 in 5 entries!
ALERT! Unable to find entry 0 in 6 entries!
ALERT! Unable to find entry 2 in 6 entries!
ALERT! Unable to find entry 4 in 6 entries!
ALERT! Unable to find entry 0 in 7 entries!
ALERT! Unable to find entry 2 in 7 entries!
ALERT! Unable to find entry 4 in 7 entries!
ALERT! Unable to find entry 0 in 8 entries!
ALERT! Unable to find entry 3 in 8 entries!
ALERT! Unable to find entry 5 in 8 entries!
ALERT! Unable to find entry 0 in 9 entries!
ALERT! Unable to find entry 3 in 9 entries!
ALERT! Unable to find entry 6 in 9 entries!
ALERT! Unable to find entry 1 in 10 entries!
ALERT! Unable to find entry 4 in 10 entries!
ALERT! Unable to find entry 7 in 10 entries!
ALERT! Unable to find entry 1 in 11 entries!
ALERT! Unable to find entry 4 in 11 entries!
ALERT! Unable to find entry 7 in 11 entries!
ALERT! Unable to find entry 9 in 11 entries!
ALERT! Unable to find entry 1 in 12 entries!
ALERT! Unable to find entry 3 in 12 entries!
ALERT! Unable to find entry 5 in 12 entries!
ALERT! Unable to find entry 8 in 12 entries!
ALERT! Unable to find entry 10 in 12 entries!
ALERT! Unable to find entry 1 in 13 entries!
ALERT! Unable to find entry 3 in 13 entries!
ALERT! Unable to find entry 5 in 13 entries!
ALERT! Unable to find entry 7 in 13 entries!
ALERT! Unable to find entry 9 in 13 entries!
ALERT! Unable to find entry 11 in 13 entries!
35 tests failed!
嗯。实际上,这并不能证明OP的代码。
但是,这表明
“off by 1”可以从根本上破坏二分搜索。
如何设计蛮力测试来发现此类错误。
因此,这有望帮助 OP 自己找到算法中的错误(这实际上对他来说更有价值)。