我有一個排序陣列雙。 目標是在數組中找到索引。 其中包含值< =搜索值。BinarySearch如何找到兩個鄰居之間的數組值?
例如,該數組包含索引範圍[0..4]的數字{0, 5, 12, 34, 100}
。
搜索值= 25。我想要索引= 2(發生的範圍在12和34之間)
我不明白在這種情況下將如何運行二進制搜索。
public class MyComparer : IComparer<double>
{
public int Compare(double x, double y)
{
//<-------- ???
}
}
public double[] spline_x;
MyComparer cmpc = new MyComparer();
int i=Array.BinarySearch(spline_x, x, cmpc);
謝謝。 我有點簡化: \t \t \t int i = Array.BinarySearch(spline_x,x); \t \t \t如果(I <0) \t \t \t { \t \t \t \t I =〜I; \t \t \t \t i--; \t \t \t} \t \t \t如果(I> = 0) \t \t \t { \t \t \t} – Mixer 2013-02-18 07:08:37
@Mixer記住,那遞減後* I *您可以的指標得到'-1'數組的最後一項 – 2013-02-18 07:17:52