Created
October 15, 2020 17:15
-
-
Save Irwin1985/07577bd8be44458eb90820435bbbae54 to your computer and use it in GitHub Desktop.
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
clear | |
Dimension aNum(1) | |
For i = 1 to 100 | |
Dimension aNum(i) | |
aNum(i) = i * 2 | |
EndFor | |
?binary_search(@aNum, 1, Alen(aNum, 1), 10) | |
* Binary search | |
Function binary_search(aItems, nIndex, nLength, nValue) | |
If nLength >= nIndex | |
nMiddle = nIndex + (nLength - nIndex) | |
Do case | |
case nValue == aItems[nMiddle] | |
Return nMiddle | |
Case aItems[nMiddle] > nValue | |
Return binary_search(@aItems, nIndex, nMiddle - 1, nValue) | |
Otherwise | |
Return binary_search(@aItems, nMiddle + 1, nLength, nValue) | |
EndCase | |
EndIf | |
Return -1 && not found. | |
EndFunc |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment