-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.js
39 lines (34 loc) · 957 Bytes
/
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
var values = [3, 14, 22, 39, 44, 61, 73, 99, 111, 126];
function search(number) {
for (i = 0; i < 6; i++) {
if (number == values[i]) {
return i;
}
}
return -1;
}
function binarySearch(number) {
let start, end;
let middle;
let steps = 0;
start = 0;
end = 9;
while (start <= end) {
middle = parseInt((start + end)/2);
steps = steps + 1;
if (number == values[middle]) {
console.log(`I found this number in ${steps} steps.`)
return middle;
} else {
if (number > values[middle]) {
start = middle +1;
} else {
end = middle - 1;
}
}
}
console.log(`We didn't found this number in ${steps} steps.`)
return -1;
}
console.log(binarySearch(22));
console.log(binarySearch(50));