8

I have a largish array of string that I want to use as a look-up.

I am using in_array(), but I suspect its doing a simple loop through - does anyone know whether the in_array() algo uses a bsearch algo?

1

3 Answers 3

11

in_array() is O(n). Also see List of Big-O for PHP functions

Sign up to request clarification or add additional context in comments.

Comments

5

Since it doesn't require the array to be sorted, I don't see how it could do a binary search.

1 Comment

I know it doesn't but to be fair, the OP may have thought that PHP was maintaining a sort-order under-the-hood.
4

in_array() uses a linear (O(n)) search rather than a binary (O(log n)) search.

If you want O(log n) or better I would suggest you either put the values you want to search as the keys in an array or you create an index structure that effectively does the same thing.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.