This article brings you relevant knowledge about javascript, which mainly introduces related issues about how JavaScript implements hash tables, and encapsulates the entire structure of the array into which the final data is inserted. , what you get is the hash table, I hope it will be helpful to everyone.
Related recommendations: javascript learning tutorial
Two common solutions to resolve conflicts:
As shown in the figure below, we will perform the remainder operation on 10 for each number, then the remainder The range 0~9 is used as the subscript value of the array. Moreover, the position corresponding to each subscript value in the array no longer stores a number, but stores an array or linked list## composed of numbers that have the same remainder after a remainder operation. #.
Summary: The way to resolve conflicts with the chain address method is that the data stored in each array unit is no longer A single data , but a chain. The commonly used data structure for this chain is array or linked list. The search efficiency of the two data structures is equivalent (because the elements of the chain are generally Not too much).
Looking for blank cells To place conflicting data items.
chain address method.
Excellent hash functionThe advantage of a hash table is its speed, so the hash function cannot use complex algorithms that consume high performance. One way to improve speed is tominimize multiplications and divisions in the hash function.
A high-performance hash function should have the following two advantages:Horner's Law: In China, Horner's Law is also called Qin Jiushao's Algorithm. The specific algorithm is:
When finding the value of a polynomial, First, calculate the value of the linear polynomial in the innermost bracket, and then calculate the value of the linear polynomial layer by layer from the inside out. This algorithm converts the value of n degree polynomial f(x) into the value of n degree polynomials.
Before transformation:
After transformation:
If big O is used to represent the time complexity, it will directly drop from O(N2) before transformation to O(N).
In order to ensure that the data is evenly distributed in the hash table, when we need touse constants, try to use Prime numbers; For example: the length of the hash table, the base of Nth power, etc.
HashMap in Java uses the chain address method, and the hashing method uses the formula: index = HashCode (key) & (Length-1)
That is to say, the data is converted into binary for and operations instead of remainder operation. In this way, the computer directly operates on binary data, which is more efficient. However, JavaScript will have problems when performing and operations called big data, so the remainder operation will still be used when using JavaScript to implement hashing.
function HashTable() { // 存放相关的元素 this.storage = []; // 存了多少数据 this.count = 0; // 用于标记数组中一共存放了多少个元素 this.limit = 7; /* 设计哈希函数 ①将字符串转成比较大的数字 ②将大的数字hashCode压缩到数组范围之内 */ HashTable.prototype.hashFunction = function (str, size) { var hashCode = 0; //秦九韶算法(霍纳算法) // 哈希表的长度、N次幂的底数等尽量选取质数 for (var i = 0; i this.limit * 0.75) { var newLimit = this.limit * 2; var prime = this.getPrime(newLimit); this.resize(prime); } }; // 获取 HashTable.prototype.get = function (key) { var index = this.hashFunction(key, this.limit); var bucket = this.storage[index]; if (bucket == null) return null; for (var i = 0; i 7 && this.count 0 ? false : true; }; // size HashTable.prototype.size = function () { return this.count; }; // toString HashTable.prototype.toString = function () { var str = ''; for (var i = 0; i <p><detailed introduction to how javascript implements hash tables src="https://Detailed%20introduction%20to%20how%20JavaScript%20implements%20hash%20tables.php.cn/upload/article/000/000/067/4ca10f86d3fe737c57c2ff0ae29c077f-3.png" alt="Detailed introduction to how JavaScript implements hash tables"></detailed></p><p>Related recommendations: <a href="https://www.php.cn/course/list/17.html" target="_blank">javascript learning tutorial</a><br></p>
The above is the detailed content of Detailed introduction to how JavaScript implements hash tables. For more information, please follow other related articles on the PHP Chinese website!