世界上最伟大的投资就是投资自己的教育
https://zh.wikipedia.org/wiki/%E4%BA%8C%E5%88%86%E6%90%9C%E5%B0%8B%E6%BC%94%E7%AE%97%E6%B3%95
const search = (val, arr) => {
for (let i = 0; i < arr.length; i++) {
console.log(i);
if (val === arr[i]) {
return i;
}
}
return -1;
};
const binary = (val, arr) => {
let lower = 0;
let upper = arr.length - 1;
while (lower <= upper) {
const middle = parseInt((lower + upper) / 2);
if (val === arr[middle]) {
return middle;
}
if (val < arr[middle]) {
upper = middle - 1;
} else {
lower = middle + 1;
}
}
return -1;
};
const values = [1, 3, 4, 7, 9, 10, 11, 14, 17, 20, 23];
console.log(binary(14, values));
0 条回复
暂无回复~~
▬▬▬▬▬▬ 联系我 👋 ▬▬▬▬▬▬
微信:qiuzhi99pro
b 站:https://space.bilibili.com/31152817
知乎:https://www.zhihu.com/people/rails365
Youtube:https://www.youtube.com/channel/UCA-Jkgr40A9kl5vsIqg-BIg
© 汕尾市求知科技有限公司 | Rails365 Gitlab | 知乎 | b 站 | csdn
粤公网安备 44152102000088号 | 粤ICP备19038915号
Top