title: Creating A Better Hash Function
body: <pre><code>#include &lt;iostream&gt;
  #include &lt;iomanip&gt;
  #include &lt;string&gt;
  #include &lt;vector&gt;
  
  using namespace std;
  
  class Item {
  public:
      Item(const string &amp; v): value(v), next(0) { }
      string value;
      Item * next;
  };
  
  int hash_function(const string &amp; s)
  {
      unsigned int hashval = 0;
      int i = s.length();
      while (i &gt; 0)
  {
          hashval += s[--i];
  }       
  return hashval%101;
  }
  
  main()
  {
      string name;
      int index;
      Item * p;
  
      vector&lt;Item *&gt; bucket(101);
  
      for (index = 0; index &lt; 101; index++)
          bucket[index] = 0;
  
      while (cin &gt;&gt; name) {
          p = new Item(name);
          index = hash_function(name);
  
          // push front
          if (bucket[index] != 0)
              p-&gt;next = bucket[index];
          bucket[index] = p;
      }
  
      for (index = 0; index &lt; 101; index++)
          if (bucket[index] != 0) {
              cout &lt;&lt; setw(3) &lt;&lt; index &lt;&lt; ": ";
              p = bucket[index];
              while (p != 0) {
                  cout &lt;&lt; p-&gt;value &lt;&lt; " ";
                  p = p-&gt;next;
              }
              cout &lt;&lt; endl;
          }
  
      Item * temp;
      for (index = 0; index &lt; 101; index++) {
          p = bucket[index];
          while (p != 0) {
              temp = p;
              p = p-&gt;next;
              delete temp;
          }
      }
  }
  </code></pre>
  
  <p>which contains two very simple hash functions. I'm trying to work on the one which is not commented out, as it seems like the better of the two when tested. I want a set of names that is input to be distributed evenly in it's own bucket and so far, that seems to be working, with the exception of names which begin with the same letter. For example, Amy and Alice will appear in the same bucket and so on.</p>
  
  <p>Here is a sample input/output:</p>
  
  <pre><code>Alice
  Amy  
  Barry
  Carrie
  David
  Garret 
  Edward
  Henry
  Ingrid
  Fred
   65: Amy Alice 
   66: Barry 
   67: Carrie 
   68: David 
   69: Edward 
   70: Fred 
   71: Garret 
   72: Henry 
   73: Ingrid 
  </code></pre>
  
  <p>What can I add to my algorithm that would allow Amy and Alice to be placed in their own bucket?</p>