Cover Data Structures and Algorithms with Object-Oriented Design Patterns in Java
next up previous contents index

Middle Square Method

In this section we consider a hashing method which avoids the use of division. Since integer division is usually slower than integer multiplication, by avoiding division we can potentially improve the running time of the hashing algorithm. We can avoid division by making use of the fact that a computer does finite-precision integer arithmetic. For example, all arithmetic is done modulo W where tex2html_wrap_inline61190 is a power of two such that w is the word size  of the computer.

The middle-square hashing method   works as follows. First, we assume that M is a power of two, say tex2html_wrap_inline61156 for some tex2html_wrap_inline58520. Then, to hash an integer x, we use the following hash function:

displaymath61182

Notice that since M and W are both powers of two, the ratio tex2html_wrap_inline61206 is also a power two. Therefore, in order to multiply the term tex2html_wrap_inline61208 by M/W we simply shift it to the right by w-k bits! In effect, we are extracting k bits from the middle of the square of the key--hence the name of the method.

The following code fragment illustrates the middle-square method of hashing:

public class MiddleSquareMethod
{
    static final int k = 10; // M==1024
    static final int w = 32;

    public static int h (int x)
	{ return (x * x) >>> (w - k); }
}
Since x is an int, the product x * x is also an an int. In Java, an int represents a 32-bit quantity and the product of two ints is also a 32-bit quantity. The final result is obtained by shifting the product w-k bits to the right, where w is the number of bits in an integer. Note that we use the Java arithmetic right-shift operator, >>>, which inserts zeroes on the left. Therefore, the result always falls between 0 and M-1.

The middle-square method does a pretty good job when the integer-valued keys are equiprobable. The middle-square method also has the characteristic that it scatters consecutive keys nicely. However, since the middle-square method only considers a subset of the bits in the middle of tex2html_wrap_inline61224, keys which have a large number of leading zeroes will collide. For example, consider the following set of keys:

displaymath61183

This set contains all keys x such that tex2html_wrap_inline61228. For all of these keys h(x)=0.

A similar line of reasoning applies for keys which have a large number of trailing zeroes. Let W be an even power of two. Consider the set of keys

displaymath61184

The least significant w/2 bits of the keys in this set are all zero. Therefore, the least significant w bits of of tex2html_wrap_inline61224 are also zero and as a result h(x)=0 for all such keys!


next up previous contents index

Bruno Copyright © 1998 by Bruno R. Preiss, P.Eng. All rights reserved.