-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinarySearch.js
More file actions
62 lines (37 loc) · 1.39 KB
/
Copy pathBinarySearch.js
File metadata and controls
62 lines (37 loc) · 1.39 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
57
58
59
60
// https://leetcode.com/problems/binary-search
// Itertative Solution
let Binarysearch1 = function(nums, target) {
let left = 0;
let right = nums.length - 1;
let pivot;
while(left <= right) {
// Define the middle of the array
pivot = left + Math.floor(right - left / 2);
// Check if the target is found
if( nums[pivot] == target) return pivot;
// if the target is bigger than the current middle, search at the right half of the array
if (nums[pivot] < target) left = pivot + 1;
// if not search at the left half of it
else right = pivot - 1;
}
// if no target found in the array print -1
return -1
};
// Recursive Solution
let Binarysearch2 = function(nums, left, right, target){
if(left <= right){
// Define the middle
let pivot = left + Math.floor((right - left) / 2);
// check if target found
if(nums[pivot] == target ) return pivot;
// if target smaller than current middle, check the left subbarray
if(nums[pivot] > target) return Binarysearch2(nums, left, pivot - 1 ,target);
// if not, check the right subarray
else return Binarysearch2(nums, pivot + 1, right, target );
}
// target not found.
return -1;
}
var arr = [0,3,5,7,8,9,12];
console.log(Binarysearch1(arr, 12));
console.log(Binarysearch2(arr, 0, 6, 9));