-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinarySearch.js
More file actions
56 lines (44 loc) · 1.53 KB
/
binarySearch.js
File metadata and controls
56 lines (44 loc) · 1.53 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
55
56
// Binary Search Implementation
function binarySearch(arr, target) {
let left = 0;
let right = arr.length - 1;
while (left <= right) {
const mid = Math.floor((left + right) / 2);
if (arr[mid] === target) {
return mid; // Found the target, return index
}
if (arr[mid] < target) {
left = mid + 1; // Search right half
} else {
right = mid - 1; // Search left half
}
}
return -1; // Target not found
}
// Recursive version
function binarySearchRecursive(arr, target, left = 0, right = arr.length - 1) {
if (left > right) {
return -1; // Base case: target not found
}
const mid = Math.floor((left + right) / 2);
if (arr[mid] === target) {
return mid;
}
if (arr[mid] < target) {
return binarySearchRecursive(arr, target, mid + 1, right);
} else {
return binarySearchRecursive(arr, target, left, mid - 1);
}
}
// Example usage
const sortedArray = [1, 3, 5, 7, 9, 11, 13, 15, 17, 19];
console.log('Iterative Binary Search:');
console.log(`Index of 7: ${binarySearch(sortedArray, 7)}`);
console.log(`Index of 15: ${binarySearch(sortedArray, 15)}`);
console.log(`Index of 20: ${binarySearch(sortedArray, 20)}`);
console.log('\nRecursive Binary Search:');
console.log(`Index of 7: ${binarySearchRecursive(sortedArray, 7)}`);
console.log(`Index of 15: ${binarySearchRecursive(sortedArray, 15)}`);
console.log(`Index of 20: ${binarySearchRecursive(sortedArray, 20)}`);
// Export for use in other modules
module.exports = { binarySearch, binarySearchRecursive };