Code: Select all
DEF FNmaximum(a%())
LOCAL N%, u%(), v%() : N% = DIM(a%(),1)
DIM u%(N% >> 1), v%(N% >> 1)
REPEAT
N% = N% >> 1
v%(0 TO N%) = a%(0 TO N%) - a%(N%+1 TO 2*N%+1)
u%(0 TO N%) = v%(0 TO N%) AND &80000000
u%(0 TO N%) = u%(0 TO N%) DIV &7FFFFFFF
v%(0 TO N%) = v%(0 TO N%) + u%(0 TO N%) EOR u%(0 TO N%)
a%(0 TO N%) = a%(0 TO N%) + a%(N%+1 TO 2*N%+1) + v%(0 TO N%)
a%(0 TO N%) DIV= 2
UNTIL N% = 0
= a%(0)
You might like to try to understand the code. If you can think of a way of simplifying or improving it, I would be very interested.
I should point out that it has little practical application, other than a demonstration, because it's barely any faster than the naïve approach of looping over all the elements, comparing each with the current maximum.