Binary Search
Busca un i32[] ascendente y devuelve el índice zero-based como usize?. El resultado está ausente cuando el valor no aparece. El array debe estar ordenado de forma ascendente.
tyrus
import { binarySearch } from "tyrus:algorithm";
const index: usize? = binarySearch([1, 2, 4, 7, 9], 7);
print(index ?? 99); // 3Resultado de estudio
binarySearchInfo devuelve un SearchInfo con un snapshot independiente de la entrada y comparaciones y probes deterministas.
tyrus
interface SearchInfo {
values: i32[];
target: i32;
index: usize?;
comparisonCount: usize;
probeCount: usize;
}tyrus
import { SearchInfo, binarySearchInfo } from "tyrus:algorithm";
const info: SearchInfo = binarySearchInfo([1, 2, 4, 7, 9], 7);
print(info.values); // [1, 2, 4, 7, 9]
print(info.target); // 7
print(info.index ?? 99); // 3
print(info.comparisonCount); // 3
print(info.probeCount); // 3La búsqueda se ejecuta en O(log n) y usa espacio auxiliar O(1).
