Is there a built-in function to do binary search? Say, given a list (sorted) and a number, find the position which keeps the listed sorted when the number is inserted.

I know that LengthWhile could manage that, but it's slow.

  • $\begingroup$ This might be relevant. See also Mr. W's answer there. $\endgroup$
    – Szabolcs
    Jan 19, 2015 at 16:10

1 Answer 1


There is some built-in binary search code but not in the core language as far as I know.

  • There is BinarySearch from the Combinatorica package, which is still the function I use most often despite the fact that that package is now deprecated and loading it causes shadowing of some Symbols.

  • There is the undocumented GeometricFunctions`BinarySearch but this function does not appear to perform particularly well.

When I need greater performance I typically use a compiled form of Leonid's code from:


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.