整数列を昇順に並べ直す処理の方が面倒な気もしますけど、とりあえずスクリプトを投下します。以下は一例
#const N 256
//Step0:乱数初期化
randomize
//Step1:昇順の集合Aを作成
screen 1, 480, 320
dim a, N
repeat N-1, 1
a(cnt) = a(cnt-1)+rnd(3)
color cnt\16*16+15, 128, cnt/16*16+15
boxf cnt\16*30, cnt/16*20, cnt\16*30+29, cnt/16*20+19
color
pos cnt\16*30, cnt/16*20
mes a(cnt)
loop
//Step2:調べる値Xを作り、search命令で検索
x = rnd(N)
search y, a, x
//Step3:search命令の結果Yを出力
gsel 0
mes "X="+x
if y ! -1 {
repeat length(y)
mes "A("+y(cnt)+")="+a(y(cnt))
loop
} else {
mes "集合Aの内にXと同値な物は存在せず、ひゃっはー"
}
stop
//search命令を突っ込んだモジュール、もうチョイ短くできたかな
#module
#deffunc search array answer, array group, int target, int offset \
, local center, local block, local start, local size
dim answer, 1
center = length(group)/2
logmes "center="+center+" : group="+group(center)+" : offset="+offset
if group(center) ! target {
if group(center) > target {
start = 0
size = center
logmes "AA"
} else {
start = center+1
size = length(group)-start
logmes "BB"
}
logmes "start="+start+" : size="+size
if size ! 0 {
dim block, size
memcpy block, group, size*4, 0, start*4
search answer, block, target, offset+start
} else {
answer = -1
}
return
}
answer(0) = center+offset
repeat -1, 1
if center-cnt < 0 : break
if group(center-cnt) ! target : break
logmes "L="+length(answer)
answer(length(answer)) = center+offset-cnt
loop
repeat -1, 1
if center+cnt >= length(group) : break
if group(center+cnt) ! target : break
logmes "L="+length(answer)
answer(length(answer)) = center+offset+cnt
loop
return
#global