How to do a binary search of an NSArray

Wed, Jul 17

Ole Begemann on binary searching an NSArray:

…NSArray comes with a binary search method named indexOfObject:inSortedRange:options:usingComparator:. Using this method can greatly speed up searching but requires the array to be sorted.

And “greatly speed up” translates to:

Searching a sorted array of 100,000 elements using binary search would require at most 17 comparisons compared to the 50,000 comparisons a naive linear search would take on average.