Last active
February 25, 2022 17:40
-
-
Save 18520339/687ba9570696cfc3ae99971d3a2d1371 to your computer and use it in GitHub Desktop.
Tìm kiếm nhị phân bằng VBA
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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