partia e vjosa osmanit code example
Example: partia e vjosa osmanit
// Logarithmic Time using Binary Searchlet arr = [1, 3, 4, 5, 6, 8, 9, 11, 12, 13, 15, 16, 17, 19, 20];
function binarySearch(arr, value) {
let left = 0,
right = arr.length - 1;
while (left <= right) {
let mid = left + Math.floor((right - left) / 2);
if (arr[mid] === value) {
return mid;
} else if (arr[mid] < value) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return -1;
}
binarySearch(arr, 8); // => 5
binarySearch(arr, 7); // => -1