Kotlin Array.binarySearch()

Kotlin Array.binarySearch() Tutorial

The Array.binarySearch() function in Kotlin is used to perform a binary search on a sorted array. It returns the index of the specified element if it is found in the array; otherwise, it returns a negative value to indicate that the element is not present in the array.

This tutorial will explore the syntax of the Array.binarySearch() function and provide examples of its usage in Kotlin arrays.

Syntax

The syntax of the Array.binarySearch() function is as follows:

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

where

ParameterDescription
elementThe element to be searched for in the array.
comparatorA comparator to compare elements in the array. If null, the elements are expected to implement the Comparable interface.
fromIndexThe starting index (inclusive) in the Array to search in, 0 by default.
toIndexThe ending index (exclusive) in the Array to search in. The array size is the default value.
Parameters of Array.binarySearch() function

Examples for Array.binarySearch() function

1. Performing Binary Search on an Array of Integers

In this example, we’ll use binarySearch() to search for an integer in a sorted array of integers.

Kotlin Program

fun main() {
    val sortedNumbersArray = arrayOf(1, 3, 5, 7, 9, 11, 13, 15, 17, 19)

    val elementToSearch = 13

    // Using binarySearch() to find the index of the element
    val index = sortedNumbersArray.binarySearch(elementToSearch)

    // Printing the original array and the result
    println("Given Sorted Numbers Array\n${sortedNumbersArray.contentToString()}\n")

    if (index >= 0) {
        println("Element $elementToSearch found at index $index.")
    } else {
        println("Element $elementToSearch not found.")
    }
}

Output

Given Sorted Numbers Array
[1, 3, 5, 7, 9, 11, 13, 15, 17, 19]

Element 13 found at index 6.

2. Performing Binary Search on an Array of Strings

In this example, we’ll use Array.binarySearch() to search for a string in a sorted array of strings.

Kotlin Program

fun main() {
    val sortedStringsArray = arrayOf("apple", "banana", "grape", "orange", "pear")

    val elementToSearch = "orange"

    // Using binarySearch() to find the index of the element
    val index = sortedStringsArray.binarySearch(elementToSearch)

    // Printing the original array and the result
    println("Given Sorted Strings Array\n${sortedStringsArray.contentToString()}\n")

    if (index >= 0) {
        println("Element \"$elementToSearch\" found at index $index.")
    } else {
        println("Element \"$elementToSearch\" not found.")
    }
}

Output

Given Sorted Strings Array
[apple, banana, grape, orange, pear]

Element "orange" found at index 3.

Summary

In this tutorial, we’ve covered the Array.binarySearch() function in Kotlin, its syntax, and how to use it to perform a binary search on a sorted array. Remember that the array must be sorted, and the correct comparator should be provided for non-comparable elements.