Created
September 3, 2024 15:41
-
-
Save lgrachov/08a0201db17074abc3c854a9eda4e03d to your computer and use it in GitHub Desktop.
Short quick sort function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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