Skip to content

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: bestTime: averageTime: worstMemoryStableIn place
O(n²)O(n²)O(n²)O(1)noyes

Type Parameters ​

Type ParameterDescription
T

Parameters ​

ParameterTypeDefault valueDescription
arrayT[]undefinedArray to sort. It is modified.
compareFn?CompareFn<T>defaultCompareOrder of elements, ascending by default.

Returns ​

void

Nothing — array itself is sorted.

MIT License