Question: fun binarysearch(sortedarray: Array, findthis: String): Int { // this is binary search, a basic algorithm to find an item in // a sorted array. //
fun binarysearch(sortedarray: Array, findthis: String): Int { // this is binary search, a basic algorithm to find an item in // a sorted array. // Inputs: // sortedarray is an array of Strings, which we assume is sorted // in increasing order (alphabetically) // findthis is a String, the String to search for in sortedarray // Output: // if findthis is in the array, then the function should just // return an index value where findthis is found (because the // sortedarray could have multiple copies of findthis, any one // of them will be satisfactory for an index); the more interesting // case is what to do when findthis is not in sortedarray -- in // that case, binarysearch should return an index value k such that // findthis would be between positions k and k+1, with two exceptions, // k=-1 if findthis is smaller than anything in sortedarray and // k is the length of sortedarray if findthis is larger than everything // in sortedarray. // Note: // Kotlin already has a binary search method, see // https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/binary-search.html // but the task here is write binary search yourself, not using the // existing binary search method; it's also invalid to use other built-in // search methods, like find() -- you should just use comparison, follow // the classic binary search algorithm which has O(lg n) runtime complexity return -1 // this statement is just so the function will return an int and compile // please write code here to implement the binary search Code in Kotlin Please.
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
