Skip to content

Instantly share code, notes, and snippets.

@lgrachov
Created September 3, 2024 15:41
Show Gist options
  • Save lgrachov/08a0201db17074abc3c854a9eda4e03d to your computer and use it in GitHub Desktop.
Save lgrachov/08a0201db17074abc3c854a9eda4e03d to your computer and use it in GitHub Desktop.
Short quick sort function
function quickSort(arr) {
if (arr.length <= 1) return arr;
const pivot = arr[arr.length - 1];
const left = arr.filter(el => el < pivot);
const right = arr.filter(el => el >= pivot).slice(0, -1);
return [...quickSort(left), pivot, ...quickSort(right)];
}
/* Usage */
const array = [3, 5, 6, 9, 8, 1, 2, 7, 4, 10];
console.log(quickSort(array)); // Output: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment