sorting

insertionSort

function

insertionSort

number[]

Insertion sort maintains and grows a subset of the first i input items in sorted order

  • Having already sorted sub-array A[ :i]
  • the algorithm repeatedly swaps item A[i] with the item to its left until the left item is no larger than A[i].
@complexityO(n^2)

can require Ω(n^2) comparisons and Ω(n^2) swaps in the worst case

@param[...list]number[]
@returnsnumber[]