ts-ds / selectionSort
Function: selectionSort()
ts
function selectionSort<T>(array, compareFn?): void;Defined in: sort/selection/selection.ts:18
Sorts an array in place using selection sort.
On step i finds the smallest element of the unsorted part array[i..n-1] and swaps it with array[i]. Makes at most n - 1 swaps — useful when writing to memory is expensive.
| Time: best | Time: average | Time: worst | Memory | Stable | In place |
|---|---|---|---|---|---|
| O(n²) | O(n²) | O(n²) | O(1) | no | yes |
Type Parameters
| Type Parameter | Description |
|---|---|
T |
Parameters
| Parameter | Type | Default value | Description |
|---|---|---|---|
array | T[] | undefined | Array to sort. It is modified. |
compareFn? | CompareFn<T> | defaultCompare | Order of elements, ascending by default. |
Returns
void
Nothing — array itself is sorted.