Skip to content

Instantly share code, notes, and snippets.

@bagpack
Created June 4, 2019 07:15
Show Gist options
  • Save bagpack/8166f8328efb435acda3bb58745e480a to your computer and use it in GitHub Desktop.
Save bagpack/8166f8328efb435acda3bb58745e480a to your computer and use it in GitHub Desktop.
binary search
private fun binarySearch(list: List<Int>, key: Int, min: Int, max: Int): Int {
if (max < min) {
return -1
}
val mid = min + (max - min) / 2
return when {
list[mid] > key -> binarySearch(list, key, min, mid - 1)
list[mid] < key -> binarySearch(list, key, mid + 1, max)
else -> mid
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment