Created
September 27, 2015 05:42
-
-
Save rlbisbe/254870de557b49b50a29 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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