Scalaで文字列配列の二分探索
def search(sortedArray:Array[Any], target:String): Int = { var upper = sortedArray.length - 1 var lower = 0 while (upper >= lower) { var i = lower + (upper - lower) / 2 if (sortedArray(i) == target) return i if (sortedArray(i) < target) lower = i + 1 else upper = i - 1 } return -1 }
型をAnyにしてComparetor的なものを導入すれば更に一般的になる