Binary search: Difference between revisions

Content added Content deleted
m (→‎{{header|Sidef}}: updated code)
Line 1,662:
Both solutions use ''sublists'' and a tracking offset in preference to "high" and "low".
====Recursive Solution====
<lang groovy>def binSearchR
def binSearchR = { a, target, offset=0 ->
//define binSearchR closure.
def n = a.size()
binSearchR = { a, key, offset=0 ->
def m = n.intdiv(2)
def n = a.size()
a.empty \
? ["The insertion point is": offset] \
: a[m] > targetkey \
? binSearchR(a[0..<m], targetkey, offset) \
: a[m] < target \
? binSearchR(a[(m + 1)..<n], targetkey, offset + m + 1) \
: [index: offset + m]
}
}</lang>
 
====Iterative Solution====
<lang groovy>def binSearchI = { aList, target ->