Kotlin List binarySearch()
Syntax & Examples


Syntax of List.binarySearch()

There are 3 variations for the syntax of List.binarySearch() extension function. They are:

1.
fun <T : Comparable<T>> List<T?>.binarySearch( element: T?, fromIndex: Int = 0, toIndex: Int = size ): Int

This extension function searches this list or its range for the provided element using the binary search algorithm. The list is expected to be sorted into ascending order according to the Comparable natural ordering of its elements, otherwise the result is undefined.

2.
fun <T> List<T>.binarySearch( element: T, comparator: Comparator<in T>, fromIndex: Int = 0, toIndex: Int = size ): Int

This extension function searches this list or its range for the provided element using the binary search algorithm. The list is expected to be sorted into ascending order according to the specified comparator, otherwise the result is undefined.

3.
fun <T> List<T>.binarySearch( fromIndex: Int = 0, toIndex: Int = size, comparison: (T) -> Int ): Int

This extension function searches this list or its range for an element for which the given comparison function returns zero using the binary search algorithm.



✐ Examples

1 Example

In this example,

  • We create a list named list containing integers from 1 to 6.
  • We use the binarySearch function to find the index of element 4 in list.
  • The result, which is the index of 4 in list, is stored in index.
  • We print the index of 4 using println.

Kotlin Program

fun main(args: Array<String>) {
    val list = listOf(1, 2, 3, 4, 5, 6)
    val index = list.binarySearch(4)
    println("Index of 4: \$index")
}

Output

Index of 4: 3

2 Example

In this example,

  • We create a list named list containing strings "apple", "banana", "cherry", "grape".
  • We use the binarySearch function to find the index of string "cherry" in list.
  • The result, which is the index of "cherry" in list, is stored in index.
  • We print the index of "cherry" using println.

Kotlin Program

fun main(args: Array<String>) {
    val list = listOf("apple", "banana", "cherry", "grape")
    val index = list.binarySearch("cherry")
    println("Index of 'cherry': \$index")
}

Output

Index of 'cherry': 2

3 Example

In this example,

  • We create a list named list containing doubles 10.5, 20.5, 30.5, 40.5, 50.5.
  • We use the binarySearch function with a comparison function to find the index of an element with a value close to 25 in list.
  • The comparison function subtracts 25 from each element and returns the result.
  • The result, which is the index of an element close to 25, is stored in index.
  • We print the index of the element close to 25 using println.

Kotlin Program

fun main(args: Array<String>) {
    val list = listOf(10.5, 20.5, 30.5, 40.5, 50.5)
    val index = list.binarySearch { it.toInt() - 25 }
    println("Index of element with value close to 25: \$index")
}

Output

Index of element with value close to 25: -3

Summary

In this Kotlin tutorial, we learned about binarySearch() extension function of List: the syntax and few working examples with output and detailed explanation for each example.