-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2.binary_search.js
More file actions
45 lines (41 loc) · 1.1 KB
/
2.binary_search.js
File metadata and controls
45 lines (41 loc) · 1.1 KB
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
//binary search - O(log2n)
const array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
let count = 0
function binarySearch(item, array) {
let start = 0
let end = array.length
let middle
let found = false
let position = -1
while (found === false && end >= start) {
count++
middle = Math.floor((start + end) / 2)
if (item === array[middle]) {
position = middle
found = true
} else if (item < array[middle]) {
end = middle
} else {
start = middle
}
}
return position
}
function recursiveBinarySearch(item, array, start = 0, end = array.length) {
let middle = Math.floor((start + end) / 2)
let found = false
let position = -1
if (item === array[middle]) {
position = middle
found = true
} else if (item < array[middle]) {
end = middle
} else {
start = middle
}
if (found === true || end <= start) {
return position
}
return recursiveBinarySearch.call(null, item, array, start, end)
}
console.log(recursiveBinarySearch(9, array), count);