forto-sorter
Version:
Fast and powerful array sorting. Sort by any property in any direction with easy to read syntax.
14 lines • 314 B
text/typescript
export const bubbleSort = ({ list }, { shouldSwap, swap }) => {
let upperIndex = list.length - 1;
while (upperIndex > 0) {
let swapIndex = 0;
for (let i = 0; i < upperIndex; i += 1) {
if (shouldSwap(i, i + 1)) {
swap(i, i + 1);
swapIndex = i;
}
}
upperIndex = swapIndex;
}
return list;
};