- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的目标是编写一个 indexOf
函数,它使用二分搜索来查找数组中所需元素的索引。
我的问题是,在编写递归解决方案时,我总是会破坏调用堆栈——即使是在绝对很小的数组中。我的期望是,即使在非尾部调用优化的语言中,二分搜索也不应该破坏任何不是巨大大的数组的调用堆栈。
用英语/伪代码来说,我的想法是
middle
为数组长度除以 2,向下舍入currentValue
为数组中间的任何内容(又名 arrayToSearch[middle]
middle
这是我用 JS 编写的:
function indexOf(soughtValue, arrayToSearch) {
let middle = Math.floor(arrayToSearch.length / 2);
let currentValue = arrayToSearch[middle];
if (soughtValue === currentValue) {
return middle;
} else if (soughtValue > currentValue) {
return indexOf(soughtValue, arrayToSearch.slice(middle, arrayToSearch.length));
} else if (soughtValue < currentValue) {
return indexOf(soughtValue, arrayToSearch.slice(0, middle));
}
}
let x = indexOf("Sarah", ["Jennifer", "Sarah", "David", "Jon"]);
console.log(x); // expecting 1
但是正如我之前所说,堆栈溢出。
我仔细地重新阅读了教科书对二分搜索的解释,并且查阅了哈佛关于该算法的 CS 简介讲座。我相信我理解它的想法,并且我相当有信心 JavaScript 正在做我期望的事情。我了解堆栈溢出的概念以及为什么递归算法可能会触发堆栈溢出。
然而,一遍又一遍地重新阅读我的代码,我无法发现我搞砸并犯了逻辑错误的地方。
如果有人能启发我,我将非常感激,因为我在这里完全耗尽了自己的精神资源,却一无所获。
最佳答案
根据我上面的评论,这是一个完整的工作示例。重大变化:
start
和 end
索引传递原始数组。这是确保您返回的索引是原始数组中的索引的一种方法。-1
(就像大多数内置 indexOf
实现的行为) .当然,根据我的第一条评论,我从排序数组开始,因为二分搜索仅适用于排序输入。
function indexOf(soughtValue, arrayToSearch, start=0, end=(arrayToSearch.length-1)) {
// base case for an empty array or otherwise failing to find the element
if (start > end) {
return -1;
}
let middle = start + Math.floor((end - start) / 2);
let currentValue = arrayToSearch[middle];
if (soughtValue === currentValue) {
return middle;
} else if (soughtValue > currentValue) {
return indexOf(soughtValue, arrayToSearch, middle + 1, end);
} else if (soughtValue < currentValue) {
return indexOf(soughtValue, arrayToSearch, start, middle - 1);
}
}
let input = ["David", "Jennifer", "Jon", "Sarah"];
console.log(indexOf("David", input)); // 0
console.log(indexOf("Jennifer", input)); // 1
console.log(indexOf("Jon", input)); // 2
console.log(indexOf("Sarah", input)); // 3
console.log(indexOf("NOT THERE", input)); // -1
更新
仅供引用,这样的函数可以轻松地迭代编写,而不是使用递归。在这种情况下,我想我更喜欢迭代版本:
function indexOf(soughtValue, arrayToSearch) {
let start = 0;
let end = arrayToSearch.length - 1;
while (start <= end) {
let middle = start + Math.floor((end-start) / 2);
let currentValue = arrayToSearch[middle];
if (soughtValue > currentValue) {
start = middle + 1;
} else if (soughtValue < currentValue) {
end = middle - 1;
} else {
return middle;
}
}
// not found
return -1;
}
let input = ["David", "Jennifer", "Jon", "Sarah"];
console.log(indexOf("David", input)); // 0
console.log(indexOf("Jennifer", input)); // 1
console.log(indexOf("Jon", input)); // 2
console.log(indexOf("Sarah", input)); // 3
console.log(indexOf("NOT THERE", input)); // -1
关于javascript - 我可以获得有关二分搜索实现失败的反馈吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39039967/
我以前做过很多关系数据库设计,我认为我对其中一些设计模式有经验......但是,我想不出从哪里开始解决这个问题。 我正在创建一个健身房数据库,它将在“健身房”表中包含基本的健身房信息。 然后我将有另一
大家好,我有时需要从网站上自动执行数据收集任务。有时我需要目录中的一堆 URL,有时我需要一个 XML 站点地图(是的,我知道有很多软件和在线服务)。 无论如何,作为我之前问题的后续,我编写了一个可以
我不明白为什么,但客户端库中似乎没有机制可以为 Windows Azure 表存储并行执行许多查询。我创建了一个模板类,可以用来节省大量时间,欢迎您随意使用它。不过,如果您能将其拆开,并提供有关如何改
每次我的作业中出现这些问题中的一个时,我都会弄错...任何人都可以帮助我理解吗?还是老师的 key 关了? (我没有办法知道,因为我没有得到正确的答案,它只是让我知道我的错误。) Assume x =
我计划参加为期一周的有关该主题的类(class)。我主要参与 Java 项目,并且对 C 和 C++ 也有一定的了解。而且,我有兴趣了解有关并发编程的更多信息,并希望获得有关本类(class)的反馈。
有谁知道提交 C# 4.0 反馈的官方方法,以便 Anders 和他的团队能够获得反馈并能够对提交的内容做出回应? 最佳答案 可能是论坛here ,或(对于错误)connect (他们为 .NET 4
这是我想要实现的示例 - http://home.mcafee.com/default.aspx 我想知道如何让页面右侧的反馈标签/按钮稍微打开而不是完全滑出。然后单击,我想打开一个页面(不是 jqu
我遇到过这样的情况:我有一个托管第三方网站的 iframe。我只需要知道 iframe 已导航到其最终的“成功”url,这样我就可以做出响应。 但是,正如您所知,由于 CORS 安全问题,现代浏览器会
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 3 年前。 Improve this qu
关闭。这个问题是off-topic .它目前不接受答案。 想改进这个问题吗? Update the question所以它是on-topic用于堆栈溢出。 关闭 10 年前。 Improve thi
我使用数据库记录管理构建 JQuery/JS/PHP/mySQL 应用程序,需要在 AJAX 调用、修改后端数据库记录时向用户提供可靠且完整的反馈。恕我直言,问题是 $.ajax success: 和
要检测无效 token ,我应该多久检查一次反馈服务? 我已经使用 APNS 服务实现了一个广播系统。我打开一个连接,发送所有 APNS 消息,然后断开连接。然后我在广播完成后立即打开一个反馈连接,并
是否可以使用 shader toy 在下一帧中访问渲染图像 (GLSL)? 最佳答案 现在他们已经实现了渲染到缓冲区,所以你可以渲染到缓冲区。实际上,如果需要,您最多可以使用四个缓冲区。还有, wat
我在Delphi XE5中开发了一个数据快照服务器。 一个客户端连接到服务器。 一个客户端触发一种方法(比如说Server.ComputeTables)。 服务器正在使用ComputeTables方法
我想改进应用程序中的 AJAX 反馈(在我的模态中发布远程表单后等)。 我已经收到一些很好的反馈,显示了加载动画 $(document).ajaxStart(function(){ $('.l
我能够通过 azure-iot-sdk-python 将消息和报告属性从 iot 集线器发送到模拟设备。现在我想获得从 IoT 中心发送到设备/模块的消息的确认 (success,expired,re
我能够通过 azure-iot-sdk-python 将消息和报告属性从 iot 集线器发送到模拟设备。现在我想获得从 IoT 中心发送到设备/模块的消息的确认 (success,expired,re
我的老板想在反馈表单中添加一个选择表单,要求用户选择他/她的国家/地区。我可以通过以下方式实现这一目标: Select country = form.addItem().addSelect("
如何设置反馈,以便在输入数字时 slider 的值发生变化? JS: $('.catalog-filter-change-price_slider-range').slider({ range:
我有一个 CQRS 解决方案,它在 HTML/JavaScript 应用程序中利用 NServiceBus 和网络 worker 。 我有场景 WebAPI 发送命令 CommandHandler 更
我是一名优秀的程序员,十分优秀!