Как изначально задумывалось, но вчера не написалось:
SeedRnd MilliSecs()
Local SZ%=10000
Dim p(SZ)
Local secret=Rand(0,SZ)
Local J=-1
For I=0 To SZ
If I=secret
J=J+2
Else
J=J+1
EndIf
p(I)=J
Next
;===================
Local K#=SZ/2
Local CHK%=SZ/2
Local d%=0
Local STP=1
While K>=1
K=K/2
DebugLog "step "+STP
DebugLog "p("+CHK+")="+p(CHK)
STP=STP+1
If p(CHK)>CHK
d=-1
Else
d=1
EndIf
CHK=CHK+d*K
DebugLog "next chk="+CHK
DebugLog "-------"
Wend
DebugLog "abort"
DebugLog "----end binary"
DebugLog "CHK="+CHK
DebugLog "d = "+d
DebugLog "step "+STP
DebugLog "p("+CHK+")="+p(CHK)
If p(CHK)<>CHK
CHK=CHK-1
EndIf
CHK=CHK+1
DebugLog "result = "+CHK
DebugLog "steps: "+STP
DebugLog "secret = "+secret
WaitKey()
лог для 10000 элементов:
step 1
p(5000)=5000
next chk=7500
-------
step 2
p(7500)=7501
next chk=6250
-------
step 3
p(6250)=6251
next chk=5625
-------
step 4
p(5625)=5626
next chk=5312
-------
step 5
p(5312)=5313
next chk=5156
-------
step 6
p(5156)=5157
next chk=5078
-------
step 7
p(5078)=5079
next chk=5039
-------
step 8
p(5039)=5040
next chk=5019
-------
step 9
p(5019)=5020
next chk=5009
-------
step 10
p(5009)=5009
next chk=5014
-------
step 11
p(5014)=5014
next chk=5016
-------
step 12
p(5016)=5017
next chk=5015
-------
step 13
p(5015)=5015
next chk=5016
-------
abort
----end binary
CHK=5016
d = 1
step 14
p(5016)=5017
result = 5016
steps: 14
secret = 5016
|
14 обращений к массиву (вместо 10000)
пропущенное число 5016