Skip to content

Instantly share code, notes, and snippets.

@Irwin1985
Created October 15, 2020 17:15
Show Gist options
  • Save Irwin1985/07577bd8be44458eb90820435bbbae54 to your computer and use it in GitHub Desktop.
Save Irwin1985/07577bd8be44458eb90820435bbbae54 to your computer and use it in GitHub Desktop.
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