怎么可能当我在SortedList上使用.net BinarySearch时,它需要的时间比我在同一个列表上使用我自己的二进制搜索方法要长得多?
使用.net binarysearch:
int ipos = MyList.Keys.ToList().BinarySearch(unSearchFor);
if (ipos >= 0)
{
// exact target found at position "ipos"
return MyList[unSearchFor];
}
else
{
// Exact key not found:
// BinarySearch returns negative when the exact target is not found,
// which is the bitwise complement of the next index in the list larger than the target.
ipos = ~ipos;
try
{
return MyList.Values[ipos - 1];
}
catch
{
return null;
}
}
Run Code Online (Sandbox Code Playgroud)
我的二元搜索方法:
int nStart = 0;
int …Run Code Online (Sandbox Code Playgroud)