在学习中写过的那些优秀的、有意思的小项目集合
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

34 lines
720 B

const indexOf_BinarySearch = (dataArr: number[], num: number): number => {
let n1 = 0;
let n2 = dataArr.length - 1;
if (n1 === num) return n1;
if (n2 === num) return n2;
while (n1 <= n2) {
const mid = Math.floor((n1 + n2) / 2);
if (num > dataArr[mid]) {
n1 = mid + 1;
} else if (num < dataArr[mid]) {
n2 = mid - 1;
} else {
return mid;
}
}
return -1;
};
// const arr = [];
// for (let index = 0; index < 100000000; index++) {
// arr.push(index);
// }
// console.time("indexOf_BinarySearch");
// indexOf_BinarySearch(arr, 99999999)
// console.timeEnd("indexOf_BinarySearch");
// console.time("indexOf");
// arr.indexOf(99999999)
// console.timeEnd("indexOf");