Here's an example implementation of the binary search algorithm in TypeScript:
index.ts491 chars23 lines
The binarySearch
function takes in an array of numbers arr
and a target number target
, and returns the index of where the target number is located in the array using the binary search algorithm. If the target number is not found in the array, it returns -1.
The binary search algorithm works by repeatedly dividing the search interval in half, comparing the middle element of the interval to the target number, and eliminating the half in which the target cannot be located until the target is found or the interval is empty.
gistlibby LogSnag