forked from utimur/algs_and_structures_course
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2_binary_search.js
44 lines (40 loc) · 1.11 KB
/
2_binary_search.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
const array = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
let count = 0
function binarySearch(array, item) {
let start = 0
let end = array.length
let middle;
let found = false
let position = -1
while (found === false && start <= end) {
count += 1
middle = Math.floor((start + end) / 2);
if (array[middle] === item) {
found = true
position = middle
return position;
}
if (item < array[middle]) {
end = middle - 1
} else {
start = middle + 1
}
}
return position;
}
function recursiveBinarySearch(array, item, start, end) {
const middle = Math.floor((start + end) / 2);
count += 1
if (item === array[middle]) {
return middle
}
if (item < array[middle]) {
return recursiveBinarySearch(array, item, 0, middle - 1)
} else {
return recursiveBinarySearch(array, item, middle + 1, end)
}
}
console.log(recursiveBinarySearch(array, 15, 0, array.length))
console.log(count)
console.log(binarySearch(array, 15))
console.log(count)