Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
14
Task/Binary-search/E/binary-search.e
Normal file
14
Task/Binary-search/E/binary-search.e
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
/** Returns null if the value is not found. */
|
||||
def binarySearch(collection, value) {
|
||||
var low := 0
|
||||
var high := collection.size() - 1
|
||||
while (low <= high) {
|
||||
def mid := (low + high) // 2
|
||||
def comparison := value.op__cmp(collection[mid])
|
||||
if (comparison.belowZero()) { high := mid - 1 } \
|
||||
else if (comparison.aboveZero()) { low := mid + 1 } \
|
||||
else if (comparison.isZero()) { return mid } \
|
||||
else { throw("You expect me to binary search with a partial order?") }
|
||||
}
|
||||
return null
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue