Default comparator function used for sorting
first element to compare
second element to compare
a negative number if a < b, 0 if a = b, a positive number if a > b
Private Readonly defaultPrivate Readonly sizePrivate Readonly valuesStatic defaultStatic sortSorts the array using the InsertionSort algorithm. Static method, no need to instantiate class
the array to be sorted
the comparator function used for sorting, defaults to numerical comparison
Default comparator function used for sorting
first element to compare
second element to compare
a negative number if a < b, 0 if a = b, a positive number if a > b
a new array with the elements sorted
Generated using TypeDoc
InsertionSort is a simple sorting algorithm that builds the final sorted array one item at a time. It is much less efficient on large lists than more advanced algorithms such as quicksort, heapsort, or merge sort. However, insertion sort provides several advantages:
The algorithm iterates through the input array, growing the sorted array behind it. At each iteration, it removes one element from the input data, finds the location it belongs within the sorted list, and inserts it there. It repeats until no input elements remain.
The worst-case time complexity is O(n^2), the average-case time complexity is O(n^2), and the best-case time complexity is O(n) when the array is already sorted.
Example
Example