sort

fun <T : Comparable<T>> sort(a: Array<T>)
fun sort(a: IntArray, fromIndex: Int, toIndex: Int)
fun sort(a: LongArray, fromIndex: Int, toIndex: Int)


fun sort(a: IntArray)
fun sort(a: LongArray)

Sorts the specified array into ascending numerical order.

Parameters

a

the array to be sorted


fun sort(a: FloatArray, fromIndex: Int, toIndex: Int)

Sorts the specified range of the array into ascending order. The range to be sorted extends from the index fromIndex, inclusive, to the index toIndex, exclusive. If fromIndex == toIndex, the range to be sorted is empty.

The < relation does not provide a total order on all float values: -0.0f == 0.0f is true and a Float.NaN value compares neither less than, greater than, nor equal to any value, even itself. This method uses the total order imposed by the method Float.compareTo: -0.0f is treated as less than value 0.0f and Float.NaN is considered greater than any other value and all Float.NaN values are considered equal.

Parameters

a

the array to be sorted

fromIndex

the index of the first element, inclusive, to be sorted

toIndex

the index of the last element, exclusive, to be sorted

Throws

if fromIndex > toIndex

ArrayIndexOutOfBoundsException

if fromIndex < 0 or toIndex > a.length


fun <T> sort(a: Array<out T>, c: Comparator<out T>)

Sorts the specified array of objects according to the order induced by the specified comparator. All elements in the array must be mutually comparable by the specified comparator (that is, c.compare(e1, e2) must not throw a ClassCastException for any elements e1 and e2 in the array).

This sort is guaranteed to be stable: equal elements will not be reordered as a result of the sort.

Implementation note: This implementation is a stable, adaptive, iterative mergesort that requires far fewer than n lg(n) comparisons when the input array is partially sorted, while offering the performance of a traditional mergesort when the input array is randomly ordered. If the input array is nearly sorted, the implementation requires approximately n comparisons. Temporary storage requirements vary from a small constant for nearly sorted input arrays to n/2 object references for randomly ordered input arrays.

The implementation takes equal advantage of ascending and descending order in its input array, and can take advantage of ascending and descending order in different parts of the same input array. It is well-suited to merging two or more sorted arrays: simply concatenate the arrays and sort the resulting array.

The implementation was adapted from Tim Peters's list sort for Python (TimSort). It uses techniques from Peter McIlroy's "Optimistic Sorting and Information Theoretic Complexity", in Proceedings of the Fourth Annual ACM-SIAM Symposium on Discrete Algorithms, pp 467-474, January 1993.

Parameters

the class of the objects to be sorted
a

the array to be sorted

c

the comparator to determine the order of the array. A null value indicates that the elements' natural ordering should be used.

Throws

if the array contains elements that are not mutually comparable using the specified comparator

(optional) if the comparator is found to violate the Comparator contract


fun <T : Comparable<T>> sort(a: Array<T>, fromIndex: Int, toIndex: Int)

Sorts the specified range of the specified array of objects into ascending order, according to the natural ordering of its elements. The range to be sorted extends from index fromIndex, inclusive, to index toIndex, exclusive. (If fromIndex==toIndex, the range to be sorted is empty.) All elements in this range must implement the Comparable interface. Furthermore, all elements in this range must be mutually comparable (that is, e1.compareTo(e2) must not throw a ClassCastException for any elements e1 and e2 in the array).

This sort is guaranteed to be stable: equal elements will not be reordered as a result of the sort.

Implementation note: This implementation is a stable, adaptive, iterative mergesort that requires far fewer than n lg(n) comparisons when the input array is partially sorted, while offering the performance of a traditional mergesort when the input array is randomly ordered. If the input array is nearly sorted, the implementation requires approximately n comparisons. Temporary storage requirements vary from a small constant for nearly sorted input arrays to n/2 object references for randomly ordered input arrays.

The implementation takes equal advantage of ascending and descending order in its input array, and can take advantage of ascending and descending order in different parts of the same input array. It is well-suited to merging two or more sorted arrays: simply concatenate the arrays and sort the resulting array.

The implementation was adapted from Tim Peters's list sort for Python (TimSort). It uses techniques from Peter McIlroy's "Optimistic Sorting and Information Theoretic Complexity", in Proceedings of the Fourth Annual ACM-SIAM Symposium on Discrete Algorithms, pp 467-474, January 1993.

Parameters

a

the array to be sorted

fromIndex

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

toIndex

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

Throws

if fromIndex > toIndex or (optional) if the natural ordering of the array elements is found to violate the Comparable contract

ArrayIndexOutOfBoundsException

if fromIndex < 0 or toIndex > a.length

if the array contains elements that are not mutually comparable (for example, strings and integers).