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