Algorithm 6-3: Linear probes for resolving hash conflicts

Source: Internet
Author: User

Linear probes are another way to solve hash conflicts. The basic idea of this method is to find the next vacant space until the vacant space is found in case of a hash conflict.


Example


Insert a value S first, for example.



<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + fingerprint + 6gjpc9wpgo8cd4kpgltzybzcm9 "http://www.2cto.com/uploadfile/Collfiles/20140617/20140617083849201.jpg" alt = "\">

Insert another value H. Because H conflicts with the HA System of A, you need to find an empty position.



Vacant Space found


Insert




Code

Public class LinearProbeST
 
  
{Private static final int M = 100; private Key [] keys = (Key []) new Object [M]; private Value [] values = (Value []) new Object [M]; public LinearProbeST () {} public Value get (Key key) {int hash = hash (key); for (int I = 0; I <M; I ++) {int index = (hash + I) % M; Key key2 = keys [index]; // the Key if (key2 = null) return null cannot be found; // The key if (key. equals (key2) {return values [index] ;}} return null;} public void put (Key key, Value value) {int hash = hash (key ); for (int I = 0; I <M; I ++) {int index = (hash + I) % M; Key key2 = keys [index]; // if (key2 = null) {keys [index] = key; values [index] = value; return;} is found ;} // locate the existing value if (key. equals (key2) {values [index] = value; return ;}} private int hash (Key key) {return (key. hashCode () & 0x7fffffff) % M ;}}
 


Performance


As the amount of data increases, the speed of conflicting hash values slows down. In the case of few conflicts, the complexity of each operation is approximately 1. In the case of many conflicts, the complexity of each operation can reach N. Therefore, M = N/2 is generally used, which has the best performance without wasting space.


Knuth parking problems

There is a fixed-size parking lot where every car stops at random position I. If the parking space I is occupied, find the parking spaces I + 1 and I + 2.


The linear probe algorithm is actually a Knuth parking problem. Http://arxiv.org/abs/math/0502220



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.