-
Notifications
You must be signed in to change notification settings - Fork 0
/
704. Binary Search.js
55 lines (45 loc) · 1.34 KB
/
704. 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
45
46
47
48
49
50
51
52
53
54
55
import * as Utils from './Utils.js';
/**
* Given an array of integers nums which is sorted in ascending order, and an integer target,
* write a function to search target in nums. If target exists, then return its index.
* Otherwise, return -1.
* You must write an algorithm with O(log n) runtime complexity.
*/
class Test {
method(nums, target) {
let start = 0;
let end = nums.length - 1;
while(start <= end) {
let mid = start + Math.floor((end - start) / 2);
let midValue = nums[mid];
// Found it!
if (midValue === target) {
return mid;
}
// Take right
if (target > midValue) {
start = mid + 1;
}
// Take left
if (target < midValue) {
end = mid - 1;
}
}
return -1;
}
}
const testExecutor = new Test();
const testCase = [
{par1: [1, 5, 7, 15, 22], target: 22, result: 4},
{par1: [1, 8, 9, 99], target: 1, result: 0},
{par1: [1], target: 1, result: 0},
{par1: [1, 8, 9, 20, 75], target: 10, result: -1},
{par1: [7, 8, 9, 20, 75], target: 1, result: -1},
{par1: [6, 8, 9, 32], target: 2, result: -1},
];
testCase.forEach(testCase => {
console.log('Executing test', testCase);
const result = testExecutor.method(testCase.par1, testCase.target);
console.log('Result:', result);
console.assert(result === testCase.result);
})