为了账号安全,请及时绑定邮箱和手机立即绑定

这个二分查找中文有问题

这个二分查找中文有问题

www说 2018-12-19 16:18:41
function binarySearch(items, value){                var startIndex  = 0,                    stopIndex   = items.length - 1,                    middle      = Math.floor((stopIndex + startIndex)/2);                                 while(items[middle] != value && startIndex < stopIndex){                    //调整检索范围                    if (value < items[middle]){                        stopIndex = middle - 1;                    } else if (value > items[middle]){                        startIndex = middle + 1;                    }                    //重新计算中间值                    middle = Math.floor((stopIndex + startIndex)/2);                }                                 //判断是否找到要搜索的值                return (items[middle] != value) ? -1 : middle;            }            var items = ['中国', '德国', '美国', '日本', '法国', '意大利', '英国'];            console.log(binarySearch(items, '德国'));            console.log(binarySearch(items, '法国'));这个二分查找英文没问题,但查找中文无法找不准确,看了下这个查找的确是有点问题,谁有比较好的二分查找demo?
查看完整描述

1 回答

?
陪伴而非守候

TA贡献1757条经验 获得超8个赞

看了下你这个,二分查找是在倒序或者正序的数组里边进行查询,你这个英文是可以,中文你怎么保证倒序或者正序


查看完整回答
反对 回复 2019-01-07
  • 1 回答
  • 0 关注
  • 472 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信