InsertionSort

Insertion Sort.

Builds the sorted array one element at a time by shifting each element leftward into its correct position. Efficient for small or nearly-sorted arrays.

Time complexity : O(n²) worst/average, O(n) best (nearly sorted) Space complexity: O(1)

Functions

Link copied to clipboard
fun <T : Comparable<T>> sort(array: Array<T>)

Sorts the array in ascending natural order.

fun <T> sort(array: Array<T>, comparator: Comparator<T>)

Sorts the array using the given comparator.