Skip to content

Instantly share code, notes, and snippets.

@rlbisbe

rlbisbe/chop.scala

Created Sep 27, 2015
Embed
What would you like to do?
def chop (target: Int, searchBox: Array[Int]) : Int = {
val size = searchBox.size
if (size == 0) {
return -1
}
if (size == 1){
if(searchBox(0) == target){
return 0
} else {
return -1
}
}
val pointer = searchBox.size / 2
val searchResult = searchBox(pointer)
if(searchResult == target){
return pointer
}
if (searchResult < target){
val partialResult = chop(target, searchBox.slice(pointer, size))
if(partialResult == -1)
return -1
return pointer + partialResult
}
if (searchResult > target){
return chop(target, searchBox.slice(0, pointer));
}
return -1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment