Solución
solution.tsTypeScript
function binarySearch(numbers: number[], target: number): number {
let leftPart = 0
let rightPart = numbers.length - 1;
while (leftPart <= rightPart) {
const midPart = Math.floor((leftPart + rightPart)/2);
if ( numbers[midPart] === target ) return midPart;
if( numbers[midPart] < target) {
leftPart = midPart + 1;
} else {
rightPart = midPart - 1
}
}
return -1;
}
// No modificar: necesario para evaluar el resultado.
export { binarySearch };0respuestas