In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "what are the commonly used JS search algorithms". In the daily operation, I believe that many people have doubts about the commonly used JS search algorithms. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the questions of "what are the commonly used JS search algorithms?" Next, please follow the editor to study!
1.for cyclic search
Basic idea: iterate through the array through for, find out the index of the value to be searched in the array, and push it into the new array
The code is implemented as follows:
Const getFnRunTime = require ('. / getRuntime'); / * * Common algorithm-for round robin * @ param {*} arr * time: 7-9ms * / function searchBy (arr, value) {let result = []; for (let I = 0, len = arr.length; I
< len; i++) { if(arr[i] === value) { result.push(i); } } return result } getFnRunTime(searchBy, 6) 测试n次稳定后的结果如图: 2.forEach循环 基本思路和for循环类似: /** * 普通算法-forEach循环版 * @param {*} arr * 耗时:21-24ms */ function searchByForEach(arr, value) { let result = []; arr.forEach((item,i) =>{if (item = value) {result.push (I);}}) return result}
It takes 21-24 milliseconds, and the performance is not as good as that of the for loop (for the time being, it's essentially the same).
3.while cycle
The code is as follows:
/ * Common algorithm-while round robin * @ param {*} arr * time: 11ms * / function searchByWhile (arr, value) {let I = arr.length, result = []; while (I) {if (arr [I] = value) {result.push (I);} iMurray;} return result}
It can be seen that the performance of while and for loops is similar, and they are both excellent, but it does not mean that the performance of forEach is not good, so it will not be used. Foreach has less code compared to the for loop, but foreach relies on IEnumerable. It is less efficient than the for cycle at run time. However, it is more convenient to use foreach in cases where the number of cycles is uncertain, or the number of cycles needs to be calculated. And the code of foreach is similar to the loop of for loop after being optimized by the code of the compiler system.
4. Dichotomy search
Dichotomy searches for more application scenarios in arrays with unique and ordered values, so we won't compare its performance with for/while/forEach.
Basic idea: compare from the middle of the sequence, if the current position value is equal to the value to be searched, the search is successful; if the value to be searched is less than the current position value, look for it in the first half of the sequence; if the value you want to search is greater than the current position value, continue to search in the second half of the sequence until you find it.
The code is as follows:
/ * binary algorithm * @ param {*} arr * @ param {*} value * / function binarySearch (arr, value) {let min = 0; let max = arr.length-1; while (min value) {max = mid-1;} else {min = mid + 1;}} return 'Not Found';}
In the scene with a large amount of data, dichotomy is very efficient, but unstable, which is also a small disadvantage under big data query.
5. Hash table lookup
Hash table lookup, also known as hash table lookup, can obtain the storage location of the record without comparison by looking for keywords. It is by establishing a definite corresponding relationship f between the storage location of the record and its keywords, so that each keyword key corresponds to a storage location f (key).
Usage scenarios for hash table lookups:
The most suitable problem for solving a hash table is to find records equal to a given value.
Hash lookup is not suitable for situations where the same keyword corresponds to multiple records
It is not suitable for range search, such as looking for students aged 18 to 22.
Here I'll give you the simplest version of hashTable to make it easier for you to understand the hash:
/ * hash table * data coverage problems may occur in the following methods * / function HashTable () {var table = []; / / Hash function var loseloseHashCode = function (key) {var hash = 0; for (var item0; I)
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.