Binary Search

by Ricardo “Rudxain” Fernández Serrata

Version 5 (September 26, 2023)

Download (93 downloads)

More efficient than `indexOf` (for large arrays). It supports searching within any value that's indexed by integers and whose values can be compared (works better if all values are same-type).

The example `A` contains the 1st 32 prime numbers. The example `T`arget to search is 11, which should return 4 if the array is kept sorted in ascending order.

This won't work correctly if the container is sorted in descending order.

I added protection against infinite looping, by checking if `#A = 0` (implicitly done using `LR[1] = -1`).

For even faster performance, this uses branchless computing!

ℹ en.wikipedia.org/wiki/Binary_search_algorithm (AKA logarithmic search)

Rudxain profile: llamalab.com/automate/community/flows/42921

LICENSE: https://unlicense.org

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.