JS 二分法查找

const binarySearch = (nums, target) => {
	let min = 0
	let max = nums.length - 1
	while (min <= max) {
		let mid = Math.round((min + max) / 2)
		if (Object.is(target, nums[mid])) {	// 找到值
			return true	// 已找到
		} else if (target < nums[mid]) {
			max = mid - 1
		} else {
			min = mid + 1
		}
	}
	
	return false
}
posted @ 2021-02-05 10:47  热心码农小吕  阅读(77)  评论(0编辑  收藏  举报