Last active
May 3, 2018 01:06
-
-
Save adhrinae/6174f356dc9f4f0de121af152c2fc744 to your computer and use it in GitHub Desktop.
Quick Sort in Javascript
This file contains 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 result = []; | |
const pivot = arr[Math.floor(arr.length / 2)]; | |
const left = arr.filter(el => el < pivot); | |
const right = arr.filter(el => el > pivot); | |
return result.concat(quickSort(left), pivot, quickSort(right)); | |
} | |
const unsorted = [7, 1, 5, 2, 9, 53, 22]; | |
console.log(quickSort(unsorted)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Problem: Cannot sort duplicated values