You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.
export function binarySearch ( sortedNumbers : number [ ] , n : number ) {
// Определяем точки начала и конца поиска
let start = 0 ;
let end = sortedNumbers . length ;
while ( start < end ) {
// Находим элемент в середине массива
const middle = Math . floor ( ( start + end ) / 2 ) ;
const value = sortedNumbers [ middle ] ;
// Сравниваем аргумент с о значением в середине массива
if ( n === value ) {
return middle ;
}
// Если аргумент меньше, чем серединное значение, разделяем массив пополам
// Теперь конец массива — это е г о бывшая середина
if ( n < value ) {
end = middle ;
// Иначе началом массива становится элемент, идущий сразу за «серединой»
} else {
start = middle + 1 ;
}
}
// если искомое число не найдено, возвращаем -1
return - 1 ;
}