更好的搜索算法,以提高性能?算法、性能

2023-09-11 23:30:11 作者:流氓兔耍流氓

我有个同学在显示页面的列表,我存储的学生信息与标识和名称的对象。

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.

//我的code:

搜索=功能(数据,查询字符串) {    VAR解析度=新阵列();       对于(STU数据){            如果(stu.name.search(查询字符串)!= -1){                  res.push(STU);            }       }    返回水库; }

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

推荐答案

您可以建立一个排序的指标,并使用二进制搜索。多个指数,如果你需要多个条件,例如,要查找名称或ID。更简单的实现比一棵树。

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.

 
精彩推荐
图片推荐