Binary Search

by Ricardo Fernández Serrata

Version 3 (June 21, 2021)

Download (12 downloads)

Faster than indexOf() if the array is huge. Supports: text string if the target is an integer, dictionary where keys are decimal integers and array of text strings.

The example array contains the 1st 32 prime numbers. The example target value to search is set to 11, which should return 4 if the array is kept sorted in ascending order.

It WON'T work correctly if the container is unsorted or sorted in descending order.

I added protection against infinite looping by checking if #A equals 0.

If T = null and #A = 0, the output will be null. In any other case, the output is guaranteed to be an integer.

For even faster performance, this uses branchless computing techniques.

4.0 average rating from 1 reviews

5 stars
4 stars
3 stars
2 stars
1 star

Rate and review within the app in the Community section.