in reply to Binary searching with Perl.

If you are using the array just as a set of values, i.e. you need three operations: adding a value to the set, deleting a value and checking if the value is in the set, you can use a hash instead of a sorted array. En equivalent of the binary search algorithm is inside the implementation of hashes - so you can use it for free, with no effort.
  • Comment on A bit OT remark (was Re: Binary searching with Perl.)

Replies are listed 'Best First'.
Re: A bit OT remark (was Re: Binary searching with Perl.)
by Abigail-II (Bishop) on May 24, 2003 at 11:20 UTC
    A sorted array with binary search allows you to do things efficiently that you cannot do efficient with hashes. For instance, finding the rank of the element you are searching for, if the searched element isn't in the set, find the largest element in the set that is smaller (nearest neighbour), or doing a range query (given two values, give all elements of the set between those values).

    Hashes are fast to do member queries with, and to do updates, but hashes are very limited in what you can do with them.

    Abigail