Created
June 4, 2019 07:15
-
-
Save bagpack/8166f8328efb435acda3bb58745e480a to your computer and use it in GitHub Desktop.
binary search
This file contains hidden or 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
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