Skip to content

ts-ds / bubbleSort

Function: bubbleSort() ​

ts
function bubbleSort<T>(array, compareFn?): void;

Defined in: sort/bubble/bubble.ts:18

Sorts an array in place using bubble sort.

Repeatedly walks through the array and swaps adjacent elements that are in the wrong order. After pass k the k largest elements are in their final places. Stops early when a pass makes no swaps.

Time: bestTime: averageTime: worstMemoryStableIn place
O(n)O(n²)O(n²)O(1)yesyes

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