tags:

views:

95

answers:

3

In the below code, hash_map is automatically sorting or maybe inserting elements in a sorted order. Any ideas why it does this?? Suggestions Please?? This is NOT a homework problem, trying to solve an interview question posted on glassdoor dot com.

#include <iostream>
#include <vector>
#include <ext/hash_map>
#include <map>
#include <string.h>
#include <sstream>

using namespace __gnu_cxx;
using namespace std;

struct eqstr
{
  bool operator()(int i, int j) const
  {
    return i==j;
  }
};
typedef hash_map<int, int, hash<int>, eqstr> myHash;
int main()
{
    myHash array;
    int inputArr[20] = {1,43,4,5,6,17,12,163,15,16,7,18,19,20,122,124,125,126,128,100};

    for(int i=0;i<20;i++){
        array[inputArr[i]] = inputArr[i]; //save value
    }
    myHash::iterator it = array.begin();
    int data;
    for (; it != array.end(); ++it) {
        data =  it->first;
        cout << ":: " << data;
    }
}

//!Output ::: 1:: 4:: 5:: 6:: 7:: 12:: 15:: 16:: 17:: 18:: 19:: 20:: 43:: 100:: 122:: 124:: 125:: 126:: 128:: 163
+5  A: 

Hash map will not automatically sort your data. In fact the order is unspecified, depending on your hash function and input order. It is just that in your case the numbers turns out are sorted.

You may want to read about hash table for how this container stores the data.

A clear counter example can be created by replacing that 100 with 999999999. The result is

:: 1:: 4:: 5:: 6:: 7:: 12:: 15:: 16:: 17:: 18:: 19:: 20:: 999999999:: 43:: 122:: 124:: 125:: 126:: 128:: 163

(The actual reason is the hash_map's bucket_count is 193 and the hash function of int is an identity function, so any numbers below 193 will appear sorted.)

KennyTM
Thanks kenny. exactly what I was trying to understand. :D
reddy
Do you know if there's a way i can insert random unsorted numbers into a hash and retrieve them in sorted order with a complexity O(n)?
reddy
@reddy: Use counting sort or radix sort.
KennyTM
+1  A: 

A hash map may appear to be sorted based on a few factors:

  • The hash function returns values that are in the same order as the input, for the range of input that you are providing.
  • There are no hash collisions.

There are no guarantees that the results will be sorted, it's only a coincidence if they come out that way.

Mark Ransom
A: 

Think about how the hash function operates. A hash is always a function f:input->output that maps the input set I into a (usually smaller) output set O so that the input set is approximately uniformly distributed across the output set.

There is no requirement that order should be preserved; in fact, it's unusual that it would be preserved, because (since the output set is smaller) there will be values *i,j*that have the same hash. This is called a collision.

On the other hand, there's no reason it shouldn't. IN fact, it can be proven that there will always exist at least one sequence that will preserve the order.

But there's another possibility: if ALL the values collide, then they get stored in some other kind of data structure, like a list. It may be that these all collidge, and the other structure imposes order.

Three possibilities: hash_map happens to sort that particular sequence, or hash_map is actually implimented as an array, or the values collidge and the implementation stores collisions in a way that gives a sorted order.

Charlie Martin