24
loading...
This website collects cookies to deliver better user experience
function BinarySearch(arr, x) {
let start=0, end=arr.length-1;
// Iterate while start not meets end
while (start<=end){
// Find the mid index
let mid=Math.floor((start + end)/2);
// If element is present at mid, return True
if (arr[mid]===x){
console.log(arr[mid]);
return true;
}
// Else look in left or right half accordingly
else if (arr[mid] < x)
start = mid + 1;
else
end = mid - 1;
}
return false;
}
const arr = [1,0,90,899,6,4,67,343,901];
const result = BinarySearch(arr,10) ? 'Element found' : 'Element not found';
console.log(result);
import math
def binarysearch(Arr,element):
start = 0
end = len(Arr) - 1
while start <= end:
mid = start + (end - start) // 2;
if Arr[mid] == element:
print(Arr[element])
return True
elif Arr[mid] < element:
start = mid + 1
else:
end = mid - 1
return -1
items = [1,2,3,4,5,6,7,8,9,10]
result ='Element found' if binarysearch(items,5) else 'Element not found'
print(result)