How does the [StackOverflow] hash table work?

Source: Internet
Author: User

Original address: Http://stackoverflow.com/questions/730620/how-does-a-hash-table-work

Lane

Body:

This is an explanation for the layman.

Let's assume that you not only want to fill the library with books, but also to find them easily when you need them.

So, do you think that if a person wants to read a book and needs to know the exact title of the book to look for, it should be possible to find a book. A person with the title + librarian's help, it should be easy to find books quickly.

So, what are you going to do? Well, obviously you can hold a list to save where you put the book, but then there's a problem, you have to look up the list if you want to find a book in the library. Let's say the list is simple and good to look at, but you don't want to look up a book and check the entire list in order.

You want a thing like that, just give a title, answer the right place, and then you walk to the bookshelf and grab the book.

But how can this be achieved? You have to do some preprocessing when you fill out the book to the library.

You can't just fill out a book like that, you have to devise a clever way to save it by taking out the book's title and running out of the bookshelf and position numbers through a small program.

The use of this magical program is that someone will read the book again, and you will get the name of the bookshelf and the title of the book through this program and book, and these are the first places you put your books.

This program, called the hash algorithm or hash calculation, is a kind of algorithm with parameters, run out of a group of numbers.

Simply put, the letters, symbols and so on the number, and then calculate a result. The reality, more complicated than this, but that is the meaning.

The point of this program is that every time you enter the same title or something, the result of each output is a result, no matter how many times you lose, how long it takes.

Well, that's the basic process of hash table work.


For professionals,

First, there is the size of a number. The result of the hashing algorithm is usually in the range of some large numbers, which must be much larger than the number of tables you have stored in the table. For example, we want to save 1 million of the book, then the output can be 0 to a larger number than 1 million.

What do we do? With a computational model, we look for a number that is larger than the maximum range, and then we start with 0.

Is the algorithm of the range is 0-20, the number of 7, we find 17, we have to find 7 round, another round, then find 0,1,2,3, and finally 3.

Of course, the computational model is not always the case and requires more design and consideration. 17 except 7 + 3.

You know the book is in position 3.

This leads to new problems, conflicts. The book fills the library. The books worked out before, and when you wanted to put the book in, you found that the bookshelf and the location had already been put in the book.

There are a lot of ways to resolve conflicts, change algorithms, or find nearby places to put them. But the best way to do this is to design better ways to avoid conflict in the first place.

Finally, in many cases, you may want to put more books outside the library's reach. You need to build a bigger library. You may also be able to use the original bookshelf and position, or you may have to re-create a new bookshelf and position for the change.

How does the [StackOverflow] hash table work?

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.