forto-sorter
Version:
Fast and powerful array sorting. Sort by any property in any direction with easy to read syntax.
35 lines (28 loc) • 812 B
text/typescript
export const quickSort = ({ list }, { shouldSwap, swap }) => {
const sortRecursively = (startIndex = 0, endIndex = list.length - 1) => {
const pivotIndex = startIndex;
let lowerIndex = startIndex;
let higherIndex = endIndex;
while (lowerIndex <= higherIndex) {
while (shouldSwap(pivotIndex, lowerIndex) && lowerIndex < endIndex) {
lowerIndex += 1;
}
while (!shouldSwap(pivotIndex, higherIndex) && higherIndex > startIndex) {
higherIndex -= 1;
}
if (lowerIndex <= higherIndex) {
swap(lowerIndex, higherIndex);
lowerIndex += 1;
higherIndex -= 1;
}
}
if (startIndex < higherIndex) {
sortRecursively(startIndex, higherIndex);
}
if (lowerIndex < endIndex) {
sortRecursively(lowerIndex, endIndex);
}
return list;
};
return sortRecursively();
};