-
Notifications
You must be signed in to change notification settings - Fork 86
Expand file tree
/
Copy pathbinarySearch.js
More file actions
27 lines (21 loc) · 780 Bytes
/
binarySearch.js
File metadata and controls
27 lines (21 loc) · 780 Bytes
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
const binarySearch = (arr, searchElement) => {
// Method1) Soring Array
// arr.sort(function (a, b) {
// return a - b;
// });
// Method2) Sorting Array - Faster
const typedArray = Float32Array.from(arr);
const sortedArray = typedArray.sort();
let start = 0,
end = sortedArray.length - 1;
while (start <= end) {
const middle = Math.floor((start + end) / 2);
if (sortedArray[middle] === searchElement) return middle;
else if (sortedArray[middle] < searchElement) start = middle + 1;
else end = middle - 1;
}
return -1;
};
// console.log(binarySearch([28, 59, 68, 16], 5)); // Output: -1
// console.log(binarySearch([28, 59, 68, 16], 68)); // Output: 3
// console.log(binarySearch([28, 59, 68, 16, 1, 6, 5], 68)); // Output: 6