better search algorithm to increase the performance?
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 perfor开发者_如何学运维mance.
// 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; }
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.
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.
精彩评论