Skip to content

Instantly share code, notes, and snippets.

@18520339
Last active February 25, 2022 17:40
Show Gist options
  • Save 18520339/687ba9570696cfc3ae99971d3a2d1371 to your computer and use it in GitHub Desktop.
Save 18520339/687ba9570696cfc3ae99971d3a2d1371 to your computer and use it in GitHub Desktop.
Tìm kiếm nhị phân bằng VBA
Function BinarySearch(strArray() As String, strSearch) As Long
Dim Left As Long, Middle As Long, Right As Long
Left = 1
Right = UBound(strArray, 1)
Do While Left <= Right
Middle = Left + (Right - Left) / 2
If strSearch = strArray(Middle, 1) Then
BinarySearch = Middle
Exit Function
End If
If strSearch > strArray(Middle, 1) Then
Left = Middle + 1
Else
Right = Middle - 1
End If
Loop
BinarySearch = -1
End Function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment