Public
Edited
Jun 1, 2023
Insert cell
Insert cell
testArray = [-40, -20, -1, 1, 2, 3, 5, 7, 9, 12, 13, 20]
Insert cell
binarySearch(testArray, 13);
Insert cell
function binarySearch(arr, x) {
let start = 0;
let end = arr.length - 1;

while (start <= end) {
const mid = Math.floor((start + end) / 2);
if (arr[mid] === x) {
return mid;
} else if (arr[mid] < x) {
start = mid + 1;
} else {
end = mid - 1;
}
}

return -1;
}

Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more