binarySearch



fun binarySearch(a: Array<Any?>, key: Any): Int

Searches the specified array for the specified object using the binary search algorithm. The array must be sorted into ascending order according to the natural ordering of its elements (as by the .sort method) prior to making this call. If it is not sorted, the results are undefined. (If the array contains elements that are not mutually comparable (for example, strings and integers), it cannot be sorted according to the natural ordering of its elements, hence results are undefined.) If the array contains multiple elements equal to the specified object, there is no guarantee which one will be found.

Return

index of the search key, if it is contained in the array; otherwise, (-(*insertion point*) - 1). The insertion point is defined as the point at which the key would be inserted into the array: the index of the first element greater than the key, or a.length if all elements in the array are less than the specified key. Note that this guarantees that the return value will be >= 0 if and only if the key is found.

Parameters

a

the array to be searched

key

the value to be searched for

Throws

if the search key is not comparable to the elements of the array.


fun binarySearch(a: Array<Any?>, fromIndex: Int, toIndex: Int, key: Any): Int

Searches a range of the specified array for the specified object using the binary search algorithm. The range must be sorted into ascending order according to the natural ordering of its elements (as by the .sort method) prior to making this call. If it is not sorted, the results are undefined. (If the range contains elements that are not mutually comparable (for example, strings and integers), it cannot be sorted according to the natural ordering of its elements, hence results are undefined.) If the range contains multiple elements equal to the specified object, there is no guarantee which one will be found.

Return

index of the search key, if it is contained in the array within the specified range; otherwise, (-(*insertion point*) - 1). The insertion point is defined as the point at which the key would be inserted into the array: the index of the first element in the range greater than the key, or toIndex if all elements in the range are less than the specified key. Note that this guarantees that the return value will be >= 0 if and only if the key is found.

Since

1.6

Parameters

a

the array to be searched

fromIndex

the index of the first element (inclusive) to be searched

toIndex

the index of the last element (exclusive) to be searched

key

the value to be searched for

Throws

if the search key is not comparable to the elements of the array within the specified range.

if fromIndex > toIndex

ArrayIndexOutOfBoundsException

if fromIndex < 0 or toIndex > a.length


fun binarySearch(a: IntArray, key: Int): Int

Searches the specified array of ints for the specified value using the binary search algorithm. The array must be sorted (as by the .sort method) prior to making this call. If it is not sorted, the results are undefined. If the array contains multiple elements with the specified value, there is no guarantee which one will be found.

Return

index of the search key, if it is contained in the array; otherwise, (-(*insertion point*) - 1). The insertion point is defined as the point at which the key would be inserted into the array: the index of the first element greater than the key, or a.length if all elements in the array are less than the specified key. Note that this guarantees that the return value will be >= 0 if and only if the key is found.

Parameters

a

the array to be searched

key

the value to be searched for


fun binarySearch(a: IntArray, fromIndex: Int, toIndex: Int, key: Int): Int

Searches a range of the specified array of ints for the specified value using the binary search algorithm. The range must be sorted (as by the .sort method) prior to making this call. If it is not sorted, the results are undefined. If the range contains multiple elements with the specified value, there is no guarantee which one will be found.

Return

index of the search key, if it is contained in the array within the specified range; otherwise, (-(*insertion point*) - 1). The insertion point is defined as the point at which the key would be inserted into the array: the index of the first element in the range greater than the key, or toIndex if all elements in the range are less than the specified key. Note that this guarantees that the return value will be >= 0 if and only if the key is found.

Since

1.6

Parameters

a

the array to be searched

fromIndex

the index of the first element (inclusive) to be searched

toIndex

the index of the last element (exclusive) to be searched

key

the value to be searched for

Throws

if fromIndex > toIndex

ArrayIndexOutOfBoundsException

if fromIndex < 0 or toIndex > a.length


fun binarySearch(a: FloatArray, fromIndex: Int, toIndex: Int, key: Float): Int

Searches a range of the specified array of floats for the specified value using the binary search algorithm. The range must be sorted (as by the .sort method) prior to making this call. If it is not sorted, the results are undefined. If the range contains multiple elements with the specified value, there is no guarantee which one will be found. This method considers all NaN values to be equivalent and equal.

Return

index of the search key, if it is contained in the array within the specified range; otherwise, (-(*insertion point*) - 1). The insertion point is defined as the point at which the key would be inserted into the array: the index of the first element in the range greater than the key, or toIndex if all elements in the range are less than the specified key. Note that this guarantees that the return value will be >= 0 if and only if the key is found.

Since

1.6

Parameters

a

the array to be searched

fromIndex

the index of the first element (inclusive) to be searched

toIndex

the index of the last element (exclusive) to be searched

key

the value to be searched for

Throws

if fromIndex > toIndex

ArrayIndexOutOfBoundsException

if fromIndex < 0 or toIndex > a.length