Binary Search

by Ricardo Fernández Serrata

Version 4 (September 8, 2021)

Download (21 downloads)

Faster than indexOf() if the array is huge. Also supports: text string if the target is a codepoint, 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.

This 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 (implicitly done using `LR[1] = -1`).

For even faster performance, this uses branchless computing techniques.

ℹ en.wikipedia.org/wiki/Binary_search_algorithm

4.0 average rating from 1 reviews

5 stars
0
4 stars
1
3 stars
0
2 stars
0
1 star
0
Reports
0

Rate and review within the app in the Community section.