-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Expand file tree
/
Copy pathBinarySearch.js
More file actions
54 lines (43 loc) · 1.28 KB
/
BinarySearch.js
File metadata and controls
54 lines (43 loc) · 1.28 KB
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
/* Binary Search: https://en.wikipedia.org/wiki/Binary_search_algorithm
*
* Search a sorted array by repeatedly dividing the search interval
* in half. Begin with an interval covering the whole array. If the value of the
* search key is less than the item in the middle of the interval, narrow the interval
* to the lower half. Otherwise narrow it to the upper half. Repeatedly check until the
* value is found or the interval is empty.
*/
function binarySearchRecursive(arr, x, low = 0, high = arr.length - 1) {
if (!Array.isArray(arr) || arr.length === 0) {
return -1
}
const mid = Math.floor(low + (high - low) / 2)
if (high >= low) {
if (arr[mid] === x) {
return mid
}
if (x < arr[mid]) {
return binarySearchRecursive(arr, x, low, mid - 1)
} else {
return binarySearchRecursive(arr, x, mid + 1, high)
}
}
return -1
}
function binarySearchIterative(arr, x, low = 0, high = arr.length - 1) {
if (!Array.isArray(arr) || arr.length === 0) {
return -1
}
while (high >= low) {
const mid = Math.floor(low + (high - low) / 2)
if (arr[mid] === x) {
return mid
}
if (x < arr[mid]) {
high = mid - 1
} else {
low = mid + 1
}
}
return -1
}
export { binarySearchIterative, binarySearchRecursive }