binarySearch

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

Searches the array or the range of the array for the provided element using the binary search algorithm. The array is expected to be sorted according to the specified comparator, otherwise the result is undefined.

If the array contains multiple elements equal to the specified element, there is no guarantee which one will be found.

Since Kotlin

1.0

Return

the index of the element, if it is contained in the array within the specified range; otherwise, the inverted insertion point (-insertion point - 1). The insertion point is defined as the index at which the element should be inserted, so that the array (or the specified subrange of array) still remains sorted according to the specified comparator.

Parameters

element

the element to search for.

comparator

the comparator according to which this array is sorted.

fromIndex

the start of the range (inclusive) to search in, 0 by default.

toIndex

the end of the range (exclusive) to search in, size of this array by default.

Throws

IndexOutOfBoundsException

if fromIndex is less than zero or toIndex is greater than the size of this array.

IllegalArgumentException

if fromIndex is greater than toIndex.


fun <T> Array<out T>.binarySearch(element: T, fromIndex: Int = 0, toIndex: Int = size): Int(source)
fun ByteArray.binarySearch(element: Byte, fromIndex: Int = 0, toIndex: Int = size): Int(source)
fun ShortArray.binarySearch(element: Short, fromIndex: Int = 0, toIndex: Int = size): Int(source)
fun IntArray.binarySearch(element: Int, fromIndex: Int = 0, toIndex: Int = size): Int(source)
fun LongArray.binarySearch(element: Long, fromIndex: Int = 0, toIndex: Int = size): Int(source)
fun FloatArray.binarySearch(element: Float, fromIndex: Int = 0, toIndex: Int = size): Int(source)
fun DoubleArray.binarySearch(element: Double, fromIndex: Int = 0, toIndex: Int = size): Int(source)
fun CharArray.binarySearch(element: Char, fromIndex: Int = 0, toIndex: Int = size): Int(source)

Searches the array or the range of the array for the provided element using the binary search algorithm. The array is expected to be sorted, otherwise the result is undefined.

If the array contains multiple elements equal to the specified element, there is no guarantee which one will be found.

Since Kotlin

1.0

Return

the index of the element, if it is contained in the array within the specified range; otherwise, the inverted insertion point (-insertion point - 1). The insertion point is defined as the index at which the element should be inserted, so that the array (or the specified subrange of array) still remains sorted.

Parameters

element

the to search for.

fromIndex

the start of the range (inclusive) to search in, 0 by default.

toIndex

the end of the range (exclusive) to search in, size of this array by default.

Throws

IndexOutOfBoundsException

if fromIndex is less than zero or toIndex is greater than the size of this array.

IllegalArgumentException

if fromIndex is greater than toIndex.