我一直在尝试使用三次样条插值的应用程序对已排序的数据数组实现二进制搜索。我无法让它在所有范围内运行。例如,搜索编号 4.4 应返回范围 [4.35-4.57) 的下限 4.35。4.57 存在于 [4.35-4.57] 和 [4.57, 4.76) 等中。
Sub Binary_Search_of_Array()
Dim SplineRanges() As Variant
Dim Right As Integer
Dim Middle As Integer
Dim Left As Integer
Dim SearchNumber As Variant
SplineRanges = Array(4, 4.35, 4.57, 4.76, 5.26, 5.88)
SearchNumber = 4.4
Right = UBound(SplineRanges)
Left = LBound(SplineRanges)
Do While Left < Right
Middle = Left + (Right - Left) / 2
If SplineRanges(Middle) < SearchNumber Then
Left = Middle + 1
ElseIf SplineRanges(Middle) > SearchNumber Then
Right = Middle - 1
Else
Left = Middle
Exit Do
End If
Loop
Debug.Print SplineRanges(Left - 1); SearchNumber; SplineRanges(Left) ' Tried many different statements to return the correct bounds.
End Sub