diff --git a/kotlin/binary-search/src/main/kotlin/BinarySearch.kt b/kotlin/binary-search/src/main/kotlin/BinarySearch.kt new file mode 100644 index 0000000..707c6b3 --- /dev/null +++ b/kotlin/binary-search/src/main/kotlin/BinarySearch.kt @@ -0,0 +1,20 @@ +class BinarySearch { + companion object { + fun search(inpList: List, searchElement: Int): Int { + require(inpList.sorted() == inpList) + var leftBound = 0 + var rightBound = inpList.size - 1 + while (leftBound <= rightBound){ + val middle = Math.floor(((leftBound + rightBound)/2).toDouble()).toInt() + if (inpList[middle] < searchElement){ + leftBound = middle + 1 + } + else if (inpList[middle] > searchElement){ + rightBound = middle - 1 + } + else return middle + } + return -1 + } + } +} \ No newline at end of file