RosettaCodeData/Task/Binary-search/Groovy/binary-search-1.groovy

14 lines
394 B
Groovy
Raw Permalink Normal View History

2013-04-10 21:29:02 -07:00
def binSearchR
2017-09-23 10:01:46 +02:00
//define binSearchR closure.
binSearchR = { a, key, offset=0 ->
2013-04-10 21:29:02 -07:00
def m = n.intdiv(2)
2017-09-23 10:01:46 +02:00
def n = a.size()
2013-04-10 21:29:02 -07:00
a.empty \
2017-09-23 10:01:46 +02:00
? ["The insertion point is": offset] \
: a[m] > key \
? binSearchR(a[0..<m],key, offset) \
2013-04-10 21:29:02 -07:00
: a[m] < target \
2017-09-23 10:01:46 +02:00
? binSearchR(a[(m + 1)..<n],key, offset + m + 1) \
2013-04-10 21:29:02 -07:00
: [index: offset + m]
}