Quadratic probing - Wikipedia, the free encyclopedia
Quadratic probing operates by taking the original hash index and adding successive values of an arbitrary quadratic polynomial until an open slot is found.
For a given hash value, the indices generated by linear probing are as follows:
This method results in primary clustering, and as the cluster grows larger, the search for those items hashing within the cluster becomes less efficient.
An example sequence using quadratic probing is:
Quadratic probing can be a more efficient algorithm in a closed hash table, since it better avoids the clustering problem that can occur with linear probing, although it is not immune. It also provides good memory caching because it preserves some locality of reference; however, linear probing has greater locality and, thus, better cache performance.
http://www.sanfoundry.com/java-program-implement-hash-tables-quadratic-probing/
Read full article from Quadratic probing - Wikipedia, the free encyclopedia
Quadratic probing operates by taking the original hash index and adding successive values of an arbitrary quadratic polynomial until an open slot is found.
For a given hash value, the indices generated by linear probing are as follows:
This method results in primary clustering, and as the cluster grows larger, the search for those items hashing within the cluster becomes less efficient.
An example sequence using quadratic probing is:
Quadratic probing can be a more efficient algorithm in a closed hash table, since it better avoids the clustering problem that can occur with linear probing, although it is not immune. It also provides good memory caching because it preserves some locality of reference; however, linear probing has greater locality and, thus, better cache performance.
1. Get the key k
2. Set counter j = 0
3. Compute hash function h[k] = k % SIZE
4. If hashtable[h[k]] is empty
(4.1) Insert key k at hashtable[h[k]]
(4.2) Stop
Else
(4.3) The key space at hashtable[h[k]] is occupied, so we need to find the next available key space
(4.4) Increment j
(4.5) Compute new hash function h[k] = ( k + j * j ) % SIZE
(4.6) Repeat Step 4 till j is equal to the SIZE of hash table
5. The hash table is full
6. Stop
int quadratic_probing_insert(int *hashtable, int key, int *empty){ /* hashtable[] is an integer hash table; empty[] is another array which indicates whether the key space is occupied; If an empty key space is found, the function returns the index of the bucket where the key is inserted, otherwise it returns (-1) if no empty key space is found */ int j = 0, hk; hk = key % SIZE; while(j < SIZE) { if(empty[hk] == 1){ hashtable[hk] = key; empty[hk] = 0; return (hk); } j++; hk = (key + j * j) % SIZE; } return (-1); }
http://www.sanfoundry.com/java-program-implement-hash-tables-quadratic-probing/
class QuadraticProbingHashTable
{
private int currentSize, maxSize;
private String[] keys;
private String[] vals;
/** Constructor **/
public QuadraticProbingHashTable(int capacity)
{
currentSize = 0;
maxSize = capacity;
keys = new String[maxSize];
vals = new String[maxSize];
}
/** Function to clear hash table **/
public void makeEmpty()
{
currentSize = 0;
keys = new String[maxSize];
vals = new String[maxSize];
}
/** Function to check if hash table is full **/
public boolean isFull()
{
return currentSize == maxSize;
}
/** Function to check if hash table is empty **/
public boolean isEmpty()
{
return getSize() == 0;
}
/** Fucntion to check if hash table contains a key **/
public boolean contains(String key)
{
return get(key) != null;
}
/** Functiont to get hash code of a given key **/
private int hash(String key)
{
return key.hashCode() % maxSize;
}
/** Function to insert key-value pair **/
public void insert(String key, String val)
{
int tmp = hash(key);
int i = tmp, h = 1;
do
{
if (keys[i] == null)
{
keys[i] = key;
vals[i] = val;
currentSize++;
return;
}
if (keys[i].equals(key))
{
vals[i] = val;
return;
}
i = (i + h * h++) % maxSize;
} while (i != tmp);
}
/** Function to get value for a given key **/
public String get(String key)
{
int i = hash(key), h = 1;
while (keys[i] != null)
{
if (keys[i].equals(key))
return vals[i];
i = (i + h * h++) % maxSize;
System.out.println("i "+ i);
}
return null;
}
/** Function to remove key and its value **/
public void remove(String key)
{
if (!contains(key))
return;
/** find position key and delete **/
int i = hash(key), h = 1;
while (!key.equals(keys[i]))
i = (i + h * h++) % maxSize;
keys[i] = vals[i] = null;
/** rehash all keys **/
for (i = (i + h * h++) % maxSize; keys[i] != null; i = (i + h * h++) % maxSize)
{
String tmp1 = keys[i], tmp2 = vals[i];
keys[i] = vals[i] = null;
currentSize--;
insert(tmp1, tmp2);
}
currentSize--;
}
}
Read full article from Quadratic probing - Wikipedia, the free encyclopedia