-
-
Notifications
You must be signed in to change notification settings - Fork 96
/
Copy pathbinary-search.ts
62 lines (59 loc) · 1.84 KB
/
binary-search.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
export type Item = number | string
/**
* Iteratively search an array of sorted items for a target item and returns its index.
*
* Time complexity:
* - for an array of numbers: O(log(n)),
* - for an array of strings: O(m*log(n)),
* - where n is the number of array elements and m is the average string length.
*
* Space complexity: O(1)
*
* @param arr - Sorted array of numbers or strings to be searched
* @param target - Number or string to be searched for
* @return index of target in array (or -1 if target is not found)
*/
export const binarySearchIterative = (arr: Item[], target: Item): number => {
let lo = 0
let hi = arr.length - 1
let mid
while (lo <= hi) {
mid = Math.floor((lo + hi) / 2)
if (arr[mid] > target) {
hi = mid - 1
} else if (arr[mid] < target) {
lo = mid + 1
} else {
return mid
}
}
return -1
}
/**
* Recursively search an array of sorted items for a target item and returns its index.
*
* Time complexity:
* - for an array of numbers: O(log(n)),
* - for an array of strings: O(m*log(n)),
* - where n is the number of array elements and m is the average string length.
*
* Space complexity: O(log(n))
*
* @param arr - Sorted array of numbers or strings to be searched
* @param target - Number or string to be searched for
* @return index of target in array (or -1 if target is not found)
*/
export const binarySearchRecursive = (arr: Item[], target: Item): number => {
const search = (arr: Item[], target: Item, lo: number, hi: number): number => {
if (lo > hi) return -1
const mid = Math.floor((lo + hi) / 2)
if (arr[mid] > target) {
return search(arr, target, lo, mid - 1)
} else if (arr[mid] < target) {
return search(arr, target, mid + 1, hi)
} else {
return mid
}
}
return search(arr, target, 0, arr.length - 1)
}