更好的搜索算法,以提高性能? [英] better search algorithm to increase the performance?

查看:94
本文介绍了更好的搜索算法,以提高性能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

这篇关于更好的搜索算法,以提高性能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆