-
Notifications
You must be signed in to change notification settings - Fork 1
/
searchTarget.ts
62 lines (49 loc) · 1.48 KB
/
searchTarget.ts
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
/* 1 way: O(n) time complexity */
const searchTarget_n = (numbers: number[], target: number): number => {
for (let i = 0; i < numbers.length; i++) {
const element = numbers[i];
if (element === target) {
return i;
}
}
return -1;
};
/* 2 way: O(log n) time complexity */
const searchTarget_logn = (numbers: number[], target: number): number => {
let leftIndex = 0;
let rightIndex = numbers.length - 1;
if (target < numbers[leftIndex] || target > numbers[rightIndex]) {
return -1;
}
while (true) {
if (target === numbers[leftIndex]) {
return leftIndex;
}
if (target === numbers[rightIndex]) {
return rightIndex;
}
if (rightIndex - leftIndex <= 1) {
return -1;
}
const middle = Math.floor((rightIndex - leftIndex) / 2);
if (target > numbers[middle]) {
leftIndex = middle + 1;
} else if (target < numbers[middle]) {
rightIndex = middle - 1;
} else {
return middle;
}
}
};
const numbers = [4, 8, 9, 12, 18];
const target1 = 9;
const target2 = 5;
const output1_n = searchTarget_n(numbers, target1);
const output2_n = searchTarget_n(numbers, target2);
const output1_logn = searchTarget_logn(numbers, target1);
const output2_logn = searchTarget_logn(numbers, target2);
console.log(`1 way: O(n) ${output1_n}`);
console.log(`1 way: O(n) ${output2_n}`);
console.log(`2 way: O(log n) ${output1_logn}`);
console.log(`2 way: O(log n) ${output2_logn}`);
export { searchTarget_n, searchTarget_logn };