-
Notifications
You must be signed in to change notification settings - Fork 266
/
BinarySearch.js
69 lines (55 loc) · 2.36 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
class BinarySearch {
iterative(key, orderedVector) {
if (!(orderedVector instanceof Array) && typeof key !== 'number')
throw new Error("it is necessary to validate the types of data reported.");
let inferiorLimit = 0; // first vector index in Javascript
let upperLimit = orderedVector.length - 1; // last index of the vector in Javascript
let middle;
while (inferiorLimit <= upperLimit) {
middle = parseInt((inferiorLimit + upperLimit) / 2);
if (key === orderedVector[middle])
return middle;
else if (key < orderedVector[middle])
upperLimit = middle - 1;
else
inferiorLimit = middle + 1;
}
return -1; // Not found
}
recursive(key, orderedVector, leftInferiorLimit, rightUpperLimit) {
if (typeof key !== 'number' && !(orderedVector instanceof Array) && typeof leftInferiorLimit !== 'number' && typeof rightUpperLimit !== 'number')
throw new Error("it is necessary to validate the types of data reported.");
const middle = parseInt((leftInferiorLimit + rightUpperLimit) / 2);
if (key === orderedVector[middle])
return middle;
if (leftInferiorLimit >= rightUpperLimit)
return -1;// Not found
else if (key > orderedVector[middle]) // 10 < 9
return this.recursive(key, orderedVector, middle + 1, rightUpperLimit);
else
return this.recursive(key, orderedVector, leftInferiorLimit, middle - 1);
}
}
function main() {
let vector = []
for (let index = 1; index <= 50; index++)
vector.push(index)
const instance = new BinarySearch();
const wantedValue = 1;
try {
const resultIterative = instance.iterative(wantedValue, vector);
const resultRecursive = instance.recursive(wantedValue, vector, 0, vector.length);
const getMessage = value => {
if (value === -1)
return "Value not found";
else
return `Value found in position ${value}`;
}
console.log(`Wanted Value -> ${wantedValue}`);
console.log(`Iterative -> ${getMessage(resultIterative)}`);
console.log(`Recursive -> ${getMessage(resultRecursive)}`);
} catch (error) {
console.error(error.message);
}
}
main();