Beefy Boxes and Bandwidth Generously Provided by pair Networks Joe
Problems? Is your data what you think it is?
 
PerlMonks  

Re^3: What makes an array sorted and a hash unsorted?

by herveus (Parson)
on Jun 02, 2009 at 12:32 UTC ( #767574=note: print w/ replies, xml ) Need Help??


in reply to Re^2: What makes an array sorted and a hash unsorted?
in thread What makes an array sorted and a hash unsorted?

Howdy!

Hash keys have no predefined ordering. Any given call to keys() will present the keys as a list which will, at that instant, have an order, but that particular ordering is neither persistent nor stable nor predictable in the face of insertions and deletions. Rapid searching for hash keys has nothing to do with ordering, but rather with the nature of hash functions. Ideally, any given key value hashes to a unique value in the hash that can be found (or the existence of tested for) in constant time. Searching an ordered list is going to be O(log n) or worse.

yours,
Michael


Comment on Re^3: What makes an array sorted and a hash unsorted?
Re^4: What makes an array sorted and a hash unsorted?
by JavaFan (Canon) on Jun 02, 2009 at 15:03 UTC
    Searching an ordered list is going to be O(log n) or worse.
    If that were true, finding an item in an array would take logarithmic time (this thread has already established arrays are sorted). It doesn't of course, given the key, it takes constant time to find something in an array.

    Just because binary search on a list of sorted values is logarithmic doesn't mean any search is logarithmic.

      Howdy!

      You appear to be conflating being ordered with being sorted and searching with simple access.

      Arrays are ordered. The elements have a defined order. Orderedness and sortedness are not the same thing. One speaks to the intrinsic sequencing of the elements of a collection (orderedness) while the other speaks to an externally imposed sequencing of the elements of a collection (sortedness).

      Searching for an element is different from accessing an element. In the first case, you don't know which element is the target, so you have to find it. Accessing a specific element means you have the index of the item already. For a hash, the index is a string; for an array, the index is an integer.

      You cannot speak generally of the intrinsic sequence of the elements of a hash. It is not a fixed quantity. Adding or removing elements can change the sequence in ways that are not readily predictable. Iterating over the elements first involves collecting the set of keys. That act turns them into a list which has an intrinsic order, but it's no longer a hash or a set. The set of keys has become an ordered n-tuple (where n is the number of keys), but it's no longer a set.

      yours,
      Michael
        You appear to be conflating being ordered with being sorted and searching with simple access.
        I'm just following the terminology set down by the OP. Let me recall the name of the topic: What makes an array sorted and a hash unsorted?

        Surely you don't suggest that ikegami thinks all arrays are sorted the way you describe sorted?

        You cannot speak generally of the intrinsic sequence of the elements of a hash. It is not a fixed quantity
        It most certainly is. Each hash element is mapped to a bucket using a fixed function: hash(key,seed,nr_of_bufs). key is the key to be hashed, seed is fixed at program start, and nr_of_bufs is the current number of buffers used. But there's no randomness involved in the hash function. Given the same input parameters, the result will always be the same. It's a deterministic function. Arrays can be seen as a special case, where the "hash" function is just sub hash {0+$_[0]}.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://767574]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (8)
As of 2014-04-18 06:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (462 votes), past polls