想知道我是否可以加快搜索速度。 我需要构建一个必须由许多UI屏幕使用的功能 我有一个工作,但我需要确保我实施一个快速algoritim如果你喜欢
这就像增量搜索。
用户键入要搜索的单词,例如
const string searchFor = "Guinea";
const char nextLetter = ' '
它在列表中查找并返回2条记录
"Guinea and Guinea Bissau "
用户键入要搜索的单词,例如
const string searchFor = "Gu";
const char nextLetter = 'i'
returns
3 results.
这是功能,但我想加快速度。 这种搜索有一种模式吗?
class Program
{
static void Main()
{
//Find all countries that begin with string + a possible letter added to it
//const string searchFor = "Guinea";
//const char nextLetter = ' '; //returns 2 results
const string searchFor = "Gu";
const char nextLetter = 'i';
List<string> result = FindPossibleMatches(searchFor, nextLetter);
result.ForEach(x=>Console.WriteLine(x)); //returns 3 results
Console.Read();
}
/// <summary>
/// Find all possible matches
/// </summary>
/// <param name="searchFor">string to search for</param>
/// <param name="nextLetter">pretend user as just typed a letter</param>
/// <returns></returns>
public static List<string> FindPossibleMatches (string searchFor, char nextLetter)
{
var hashedCountryList = new HashSet<string>(CountriesList());
var result=new List<string>();
IEnumerable<string> tempCountryList = hashedCountryList.Where(x => x.StartsWith(searchFor));
foreach (string item in tempCountryList)
{
string tempSearchItem;
if (nextLetter == ' ')
{
tempSearchItem = searchFor;
}
else
{
tempSearchItem = searchFor + nextLetter;
}
if(item.StartsWith(tempSearchItem))
{
result.Add(item);
}
}
return result;
}
/// <summary>
/// Returns list of countries.
/// </summary>
public static string[] CountriesList()
{
return new[]
{
"Afghanistan", "Albania", "Algeria", "American Samoa", "Andorra",
"Angola", "Anguilla", "Antarctica", "Antigua And Barbuda", "Argentina",
"Armenia", "Aruba", "Australia", "Austria", "Azerbaijan",
"Bahamas", "Bahrain", "Bangladesh", "Barbados", "Belarus",
"Belgium", "Belize", "Benin", "Bermuda", "Bhutan",
"Bolivia", "Bosnia Hercegovina", "Botswana", "Bouvet Island", "Brazil",
"Brunei Darussalam", "Bulgaria", "Burkina Faso", "Burundi", "Byelorussian SSR",
"Cambodia", "Cameroon", "Canada", "Cape Verde", "Cayman Islands",
"Central African Republic", "Chad", "Chile", "China", "Christmas Island",
"Cocos (Keeling) Islands", "Colombia", "Comoros", "Congo", "Cook Islands",
"Costa Rica", "Cote D'Ivoire", "Croatia", "Cuba", "Cyprus",
"Czech Republic", "Czechoslovakia", "Denmark", "Djibouti", "Dominica",
"Dominican Republic", "East Timor", "Ecuador", "Egypt", "El Salvador",
"England", "Equatorial Guinea", "Eritrea", "Estonia", "Ethiopia",
"Falkland Islands", "Faroe Islands", "Fiji", "Finland", "France",
"Gabon", "Gambia", "Georgia", "Germany", "Ghana",
"Gibraltar", "Great Britain", "Greece", "Greenland", "Grenada",
"Guadeloupe", "Guam", "Guatemela", "Guernsey", "Guiana",
"Guinea", "Guinea Bissau", "Guyana", "Haiti", "Heard Islands",
"Honduras", "Hong Kong", "Hungary", "Iceland", "India",
"Indonesia", "Iran", "Iraq", "Ireland", "Isle Of Man",
"Israel", "Italy", "Jamaica", "Japan", "Jersey",
"Jordan", "Kazakhstan", "Kenya", "Kiribati", "Korea, South",
"Korea, North", "Kuwait", "Kyrgyzstan", "Lao People's Dem. Rep.", "Latvia",
"Lebanon", "Lesotho", "Liberia", "Libya", "Liechtenstein",
"Lithuania", "Luxembourg", "Macau", "Macedonia", "Madagascar",
"Malawi", "Malaysia", "Maldives", "Mali", "Malta",
"Mariana Islands", "Marshall Islands", "Martinique", "Mauritania", "Mauritius",
"Mayotte", "Mexico", "Micronesia", "Moldova", "Monaco",
"Mongolia", "Montserrat", "Morocco", "Mozambique", "Myanmar",
"Namibia", "Nauru", "Nepal", "Netherlands", "Netherlands Antilles",
"Neutral Zone", "New Caledonia", "New Zealand", "Nicaragua", "Niger",
"Nigeria", "Niue", "Norfolk Island", "Northern Ireland", "Norway",
"Oman", "Pakistan", "Palau", "Panama", "Papua New Guinea",
"Paraguay", "Peru", "Philippines", "Pitcairn", "Poland",
"Polynesia", "Portugal", "Puerto Rico", "Qatar", "Reunion",
"Romania", "Russian Federation", "Rwanda", "Saint Helena", "Saint Kitts",
"Saint Lucia", "Saint Pierre", "Saint Vincent", "Samoa", "San Marino",
"Sao Tome and Principe", "Saudi Arabia", "Scotland", "Senegal", "Seychelles",
"Sierra Leone", "Singapore", "Slovakia", "Slovenia", "Solomon Islands",
"Somalia", "South Africa", "South Georgia", "Spain", "Sri Lanka",
"Sudan", "Suriname", "Svalbard", "Swaziland", "Sweden",
"Switzerland", "Syrian Arab Republic", "Taiwan", "Tajikista", "Tanzania",
"Thailand", "Togo", "Tokelau", "Tonga", "Trinidad and Tobago",
"Tunisia", "Turkey", "Turkmenistan", "Turks and Caicos Islands", "Tuvalu",
"Uganda", "Ukraine", "United Arab Emirates", "United Kingdom", "United States",
"Uruguay", "Uzbekistan", "Vanuatu", "Vatican City State", "Venezuela",
"Vietnam", "Virgin Islands", "Wales", "Western Sahara", "Yemen",
"Yugoslavia", "Zaire", "Zambia", "Zimbabwe"
};
}
}
}
有什么建议吗? 感谢
答案 0 :(得分:2)
是的,将所有这些放在数据库中并使用SQL查询(最好使用LIKE
子句)来完成工作。我这样说的原因是,数据库是设计的来做你想做的事情。信息存储和检索。
如果根据您的要求无法部署完整的SQL实例,您可以随时查看具有C#实现的SQLLite。
答案 1 :(得分:2)
用于前缀搜索的数据结构是TRIE。您可以在此处阅读有关数据结构的更多信息: http://en.wikipedia.org/wiki/Trie
您可以在以下链接中找到几个c#实现的trie: http://www.kerrywong.com/2006/04/01/implementing-a-trie-in-c/
https://codereview.stackexchange.com/questions/2195/is-this-a-reasonable-trie-implementation
希望这有助于提高速度。
答案 2 :(得分:1)
如果您的数据集是静态的,我可以考虑在排序的数组上使用二进制搜索来定位范围的开头和结尾。介于两者之间的是你的结果。