better search algorithm to increase the performance?

萝らか妹 提交于 2019-12-25 06:45:06

问题


I have list of students displaying in a page and I am storing the student information as object with id and name.

I want to add search box to search students if I got a scroll bar in the list.

And update the students list according to the search string.

Right now i am iterating student object array and checking the index of the search string in the name.

Is there any better algorithm to increase the performance.

// my code:

search = function(data,queryString) { var res = new array(); for(stu in data){ if(stu.name.search(queryString) != -1){ res.push(stu); } } return res; }


回答1:


You can build a sorted index and use binary search. Multiple indices if you need to search by multiple criteria, e.g. name or ID. Simpler to implement than a tree.




回答2:


You want to look for a trie-datastructure or a radix-trie or a crit-bit trie where empty nodes are compressed. You want to look for a kart-trie a special version of the radix-trie where there is only 2 edges in a node. In general a trie is good for text search algorithm, for example a dictionary. I have done an implementation of the kart-trie in php at phpclasses.org ( kart-trie ). You are welcome to download and play with it.



来源:https://stackoverflow.com/questions/5500579/better-search-algorithm-to-increase-the-performance

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!