Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
46
Task/Binary-search/Action-/binary-search.action
Normal file
46
Task/Binary-search/Action-/binary-search.action
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
INT FUNC BinarySearch(INT ARRAY a INT len,value)
|
||||
INT low,high,mid
|
||||
|
||||
low=0 high=len-1
|
||||
WHILE low<=high
|
||||
DO
|
||||
mid=low+(high-low) RSH 1
|
||||
IF a(mid)>value THEN
|
||||
high=mid-1
|
||||
ELSEIF a(mid)<value THEN
|
||||
low=mid+1
|
||||
ELSE
|
||||
RETURN (mid)
|
||||
FI
|
||||
OD
|
||||
RETURN (-1)
|
||||
|
||||
PROC Test(INT ARRAY a INT len,value)
|
||||
INT i
|
||||
|
||||
Put('[)
|
||||
FOR i=0 TO len-1
|
||||
DO
|
||||
PrintI(a(i))
|
||||
IF i<len-1 THEN Put(32) FI
|
||||
OD
|
||||
i=BinarySearch(a,len,value)
|
||||
Print("] ") PrintI(value)
|
||||
IF i<0 THEN
|
||||
PrintE(" not found")
|
||||
ELSE
|
||||
Print(" found at index ")
|
||||
PrintIE(i)
|
||||
FI
|
||||
RETURN
|
||||
|
||||
PROC Main()
|
||||
INT ARRAY a=[65530 0 1 2 5 6 8 9]
|
||||
|
||||
Test(a,8,6)
|
||||
Test(a,8,-6)
|
||||
Test(a,8,9)
|
||||
Test(a,8,-10)
|
||||
Test(a,8,10)
|
||||
Test(a,8,7)
|
||||
RETURN
|
||||
Loading…
Add table
Add a link
Reference in a new issue