这个折半递归搜索哪里错了?
var list = [1,2,3,4,5,6,7,8,9,10];
var binaryRecursiveSearch = function(list, search_num, left, right) {
var left = left || 0;
var right = right || list.length;
var middle;
var compare = function(a, b) {
console.log("compare a:"+a+", b:"+b);
if (a < b) return -1;
else if (a === b) return 0;
else return 1;
}
if (left <= right) {
middle = Math.round((left + right) / 2);
console.log("left: "+left+", right:"+right+", search_num:"+search_num);
switch (compare(list[middle], search_num)) {
case -1: return
binaryRecursiveSearch(list, search_num, middle+1, right);
case 0: return middle;
case 1: return
binaryRecursiveSearch(list, search_num, left, middle-1);
}
}
return -1;
}
binaryRecursiveSearch(list, 3);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
下面几个地方吧: