你好,我需要一些幫助,在我的圖書館。我試圖從我的List<>
執行二進制搜索,但其效果不好。Binary search for List <object>
這是我的圖書館類。
private class Library
{
List<object> library = new List<object>();
public void AddBook(string bookName, string bookAuthor, int bookIDNum)
{
//Add books to the library.
string bookEntry = bookName + " " + bookAuthor + " " + bookIDNum;
library.Add(bookEntry);
library.TrimExcess();
}
public void SearchLibrary(string bookName)
{
//Searches the library by title
library.Sort();
int low = 0;
int high = library.Count;
int mid = 0;
int steps = 0;
while(!bookName)
{
steps++;
mid = (low + high)/2;
if(bookName == library[mid])
{
return true;
}
else if(bookName < library[mid])
{
high = mid;
}
else
{
low = mid;
}
if(low > high-1 || high < low+1)
{
return false;
}
}
}
}
}
如果有更好的方法我可以做一個搜索方法,我將不勝感激,謝謝。
我只是要離開這個在這裏...:http://msdn.microsoft.com/en-us/library/w4e7fxsh(v=vs.110).aspx –
請出示樣品,使感/編譯。顯然'public void SearchLibrary(){... return true; ...}'不是有效的代碼。 –
您打算如何對未排序的數據進行二分搜索?爲什麼你的'List'包含'object'?爲什麼你的'void'方法試圖返回布爾值?爲什麼搜索方法不會返回匹配值的索引或匹配值本身? –