[JS data structure and algorithm] implement hash function

table of Contents

1. Excellent hash function

1. Continuous multiplication of powers

2. Horner's Law (Qin Jiushao Algorithm)

3. How to achieve uniform distribution.

Second, implement the hash function


1. Excellent hash function

Before constructing the hash table, we need a hash function to hash the data.

So how to implement this hash function? According to the previous blog [JS data structure] to understand the hash table , we have already understood what a hash table is and why we need to design a hash function.

In fact, it is to achieve two goals:

  • Ability to calculate quickly and obtain hashCode quickly
  • Make the elements evenly distributed in the hash table.

1. Continuous multiplication of powers

For the data storage mentioned earlier, one way is to use the power of the multiplication to obtain the hashCode

give = 7 * 27^3 + 9 * 27^2 + 22 * 27 + 5 = 144941

In this way, it is actually a polynomial, which can be reduced to:

The number of multiplications here is: n + (n-1) + ... + 1 = n(n + 1) / 2 times

The number of additions is: N times.

The time complexity obtained is (N^2 + N)/2 which is O(N^2)

2. Horner's Law (Qin Jiushao Algorithm)

Horner's law is an optimization of polynomials, so that the number of multiplications is reduced, so that the hashCode can be obtained quickly, which is transformed into the following form:

Take give as an example

The number of times of multiplication here is: N times;

The number of additions is: N times.

The time complexity obtained is (N + N) or O(N)

Therefore, the use of Horner's law can greatly improve efficiency and reduce calculation time.

3. How to achieve uniform distribution.

When designing a hash table, we already have two methods for dealing with mappings to the same subscript value, that is, to resolve conflicts, one is the chain address method, and the other is the open address method.

Regardless of the method, we'd better make the data evenly distributed in the hash table .

So when we use constants, we must use prime numbers

1. The length of the hash table .

2. The base of the Nth power (37 is often used).

Prime numbers are very important, suppose the length of the table is 10 (subscript value is 0~9)

A specific keyword is mapped to the position where the subscript value is 0, and the step size is 5, then the sequence of exploration will be 0-5-0-5...and the cycle continues.

If the length of the table is 11, then the sequence to be explored is: 0-5-10-4-9-3-8-2-7-1-6, so there will be no loops and the data can be in the hash table evenly distributed.

 

Second, implement the hash function

 Design a hash function
    1. Convert the string into a relatively large number to get hashCode.
    2. Compress this large number hashCode into the range of the array size.

 

Two parameters need to be passed in, one is the string and the other is the length of the hash table.

function hashFunc(str, size){

      //定义一个变量来存储hashCode
      var hashCode = 0;

      // 利用霍纳法则计算出hashCode的值
      // give -> Unicode编码
      for (var i = 0; i < str.length; i++) {
        hashCode = 37 * hashCode + str.charCodeAt(i); 
      }

      // 利用hashCode与哈希表的长度取余得到下标值
      var index = hashCode % size;

      return index;
}

 

Test code:

    //测试
    alert(hashFunc('abc', 7)); // 4
    alert(hashFunc('cla', 7)); // 2
    alert(hashFunc('nbr', 7)); // 1
    alert(hashFunc('kba', 7)); // 0

Summary: A good hash function is to map data to different locations as quickly as possible .

Guess you like

Origin blog.csdn.net/weixin_42339197/article/details/99544523
Recommended