38 lines
690 B
Text
38 lines
690 B
Text
.de end
|
|
..
|
|
.de array
|
|
. nr \\$1.c 0 1
|
|
. de \\$1.push end
|
|
. nr \\$1..\\\\n+[\\$1.c] \\\\$1
|
|
. end
|
|
. de \\$1.pushln end
|
|
. if \\\\n(.$>0 .\\$1.push \\\\$1
|
|
. if \\\\n(.$>1 \{ \
|
|
. shift
|
|
. \\$1.pushln \\\\$@
|
|
\}
|
|
. end
|
|
..
|
|
.
|
|
.de binarysearch
|
|
. nr min 1
|
|
. nr max \\n[\\$1.c]
|
|
. nr guess \\n[min]+\\n[max]/2
|
|
. while !\\n[\\$1..\\n[guess]]=\\$2 \{ \
|
|
. ie \\n[\\$1..\\n[guess]]<\\$2 .nr min \\n[guess]+1
|
|
. el .nr max \\n[guess]-1
|
|
.
|
|
. if \\n[min]>\\n[max] \{
|
|
. nr guess 0
|
|
. break
|
|
. \}
|
|
. nr guess \\n[min]+\\n[max]/2
|
|
. \}
|
|
\\n[guess]
|
|
..
|
|
.array a
|
|
.a.pushln 1 4 9 16 25 36 49 64 81 100 121 144
|
|
.binarysearch a 100
|
|
.br
|
|
.ie \n[guess]=0 The item \fBdoesn't exist\fP.
|
|
.el The item \fBdoes exist\fP.
|