Binary Search
Busca um i32[] crescente e retorna o índice zero-based como usize?. O resultado fica ausente quando o valor não ocorre. O array já deve estar ordenado em ordem crescente.
tyrus
import { binarySearch } from "tyrus:algorithm";
const index: usize? = binarySearch([1, 2, 4, 7, 9], 7);
print(index ?? 99); // 3Resultado de estudo
binarySearchInfo retorna um SearchInfo com snapshot independente da entrada e comparações e probes determinísticos.
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); // 3A busca executa em O(log n) e usa espaço auxiliar O(1).
