Introduction to the hash table

Source: Internet
Author: User

A hash table, also known as a hash table , is a data structure that accesses the memory storage location directly from the keyword, which is an unordered list of symbols implemented with an array. The key is stored as the index of the array and the key I in the array is its corresponding value. This allows for fast access to the value of any key. A hash table is a classic example of how the algorithm makes tradeoffs between time and space.

The lookup algorithm for a hash table is divided into two steps :

1. Using a hash function to convert a key to an array index, multiple key hashes may appear above the same index value, which is the second step.

2. Handle Collision collisions (zipper method and Linear detection method).

The concept of hash function

Strictly speaking, each type of key should have a corresponding hash function,

positive integers : In general, the remainder method is used to select an array of prime numbers m, and for any positive integer k, K is divided by M. can be effectively scattered between the 0-m-1. If M is not a prime number, it may not spread evenly.

floating-point number: in Java, the key is represented as a binary and then the save-to-rest method is used.

string : Charat () in Java returns a non-negative 16-bit integer, so long as r is small enough to cause overflow, the result will fall between 0 and M-1

1 int hash = 0; 2  for (int i = 0; i < s.length (); i++) 3     hash = (R * hash + s.charat (i))% M;
View Code

Soft cache

If the hash value is computationally time-consuming, we can cache the hash value of each key, which is the return value of Hascode () for each key that uses a hash variable to hold it.

A hash table based on zipper method

A hash table based on linear detection method

Introduction to the hash table

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.