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
//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] >
? binSearchR(a[0..<m],
: a[m] < target \
? binSearchR(a[(m + 1)..<n],
: [index: offset + m]
}
====Iterative Solution====
<lang groovy>def binSearchI = { aList, target ->
|