-
Notifications
You must be signed in to change notification settings - Fork 0
/
binarySearch.js
48 lines (47 loc) · 1.32 KB
/
binarySearch.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/**
* Created by Administrator on 2017/7/16.
*/
/**
* 算法思路:有序数组,取到数组的首和末位置。
* 然后取到中间值,判断 key与中间值关系
* 1)若相等,返回下标
* 2)若key<middle。right = middle - 1;继续迭代
* 3)若key>middle。left = middle + 1;继续迭代
* 直至就剩一个元素为止。
* @param array
* @param key
* @returns {*}
*/
function binarySearch(array,key) {
let left = 0,
right=array.length;
while (left < right) {
var middle = parseInt((left + right) / 2); //取得中间值
console.log(middle)
if (key < array[middle]) {
right = middle - 1;
if (right == left) {
if (key == array[right]){
return right
} else {
return -1
}
}
} else {
console.log('hello?')
if (key == array[middle]) {
return middle
}
left = middle + 1;
if (right == left){
if (key == array[left]){
return left
} else {
return -1
}
}
}
}
}
var arr=[2, 3, 4, 5, 15, 19, 26, 27, 36, 38, 44, 46, 47, 48, 50]
console.log(binarySearch(arr,19))