function quickSort(arr) {
if (arr.length <= 1) {
return arr;
}
const Pivot = arr[arr.length - 1];
const LeftSide = [];
const RightSide = [];
for (let index = 0; index < arr.length - 1; index++) {
if (arr[index] < Pivot) {
LeftSide.push(arr[index]);
} else {
RightSide.push(arr[index]);
}
}
return [...quickSort(LeftSide), Pivot, ...quickSort(RightSide)];
}
const a = quickSort([1, 2, 5, 7, 3, 0.99, -199, -1, 99]);
a; // [ -199, -1, 0.99, 1, 2, 3, 5, 7, 99 ]
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter