Skip to content

Instantly share code, notes, and snippets.

@rlbisbe
Created September 27, 2015 05:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rlbisbe/254870de557b49b50a29 to your computer and use it in GitHub Desktop.
Save rlbisbe/254870de557b49b50a29 to your computer and use it in GitHub Desktop.
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