[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10]
_____________________________________________
66 76 24 1 26 10 87
_____________________________________________
Load Factor = number of items/ tablesize
= 7/11
[0] ---> 66 [1] ---> 1 [2] ---> 24 [3] [4] ---> 26 [5] [6] [7] [8] [9] [10] --->10 ---> 76 --->87 Load Factor = 7/11
int hash (string s)
{
// precondition : s is uppercase letters
// postcondition : returns hashed value of s
int pos = 1;
int result = 0;
for (int i = 0; i < s.length();i++)
{
result += pos * int(s.at(i)-'A');
pos *= 10; // could argue for different multiplier
}
return result % tableSize;
}
struct node
{
int key;
// other stuff
node * next;
};
void insert (node *hashtable[],int n, node *newItem)
// precondition : hastable [0..n-1] is a hashtable
// postcondition : newItem is inserted into hashtable
// calls function hash
{
int pos = hash(newItem->key);
newItem->next = hashtable[pos];
hashtable[pos] = newItem;
}
int getSmallest( multimap < int,int> m)
{
return m.begin()->first;
}
// return key value - first field in pair
Largest Key value - find last key in multimap.
Note m.end() is BEYOND end of data so cannot use end() but can use rbegin();
which is starting point of reverse_iterator (one which goes backwardS)
int getLargest( multimap < int, int> m)
{
return m.rbegin()->first;
}
What is the efficiency of this operation? Ditto smallest.
O(1) for each
void multimapsort(int a[], int n)
{
multimap m;
for (int i = 0; i < n; i++)
m.insert(make_pair(a[i],0)); second argiment is dummy
multimap :: itertor it;
int i = 0;
for (it = m.begin(); it != m.end(); it++)
a[i++] = it->first;
}
efficiency is
Just improving understanding of multimaps here - not seriously suggesting it as an alternative. Would use sort in
#include <algorithm>
vector <int > v;
...
sort(v.begin(),v.end());
stable_sort(v.begin(),v.end());
random_shuffle(v.begin(), v.end());
...