insertionSort method

void insertionSort({
  1. Comparator<E>? comparator,
  2. int start = 0,
  3. int? end,
})

Sort this list between start (inclusive) and end (exclusive) using insertion sort.

If comparator is omitted, this defaults to calling Comparable.compareTo on the objects. If any object is not Comparable, this throws a TypeError.

Insertion sort is a simple sorting algorithm. For n elements it does on the order of n * log(n) comparisons but up to n squared moves. The sorting is performed in-place, without using extra memory.

For short lists the many moves have less impact than the simple algorithm, and it is often the favored sorting algorithm for short lists.

This insertion sort is stable: Equal elements end up in the same order as they started in.

Implementation

void insertionSort({Comparator<E>? comparator, int start = 0, int? end}) {
  collection.insertionSort(this, compare: comparator, start: start, end: end);
}