List-BinarySearch
view release on metacpan or search on metacpan
lib/List/BinarySearch.pm view on Meta::CPAN
Yes, before using this module the user should weigh the other options such as
linear searches ( C<grep> or C<List::Util::first> ), or hash based searches. A
binary search requires an ordered list, so one must weigh the cost of sorting or
maintaining the list in sorted order. Ordered lists have O(n) time complexity
for inserts. Binary Searches are best when the data set is already ordered, or
will be searched enough times to justify the cost of an initial sort.
There are cases where a binary search may be an excellent choice. Finding the
first matching element in a list of 1,000,000 items with a linear search would
have a worst-case of 1,000,000 iterations, whereas the worst case for a binary
search of 1,000,000 elements is about 20 iterations. In fact, if many lookups
will be performed on a seldom-changed list, the savings of O(log n) lookups may
outweigh the cost of sorting or performing occasional linear time inserts.
=head1 EXPORT
Nothing is exported by default. C<binsearch>, C<binsearch_pos>, and
C<binsearch_range> may be exported by listing them on the export list.
Or import all functions by specifying C<:all>.
( run in 1.533 second using v1.01-cache-2.11-cpan-71847e10f99 )