Created
February 19, 2018 05:49
-
-
Save hirenchauhan2/72f780a7425d76d6cab3d84fad72576c to your computer and use it in GitHub Desktop.
Quicksort algorithm 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
/*** | |
* Quicksort Algorithm | |
**/ | |
const quickSort = (arr) => { | |
if (arr.length <= 1) | |
return arr | |
else { | |
const len = arr.length | |
const pivot = arr[Math.floor(len / 2)]; | |
return [].concat( | |
quickSort(arr.filter(val => pivot > val)), | |
arr.filter(val => pivot == val), | |
quickSort(arr.filter(val => pivot < val)) | |
) | |
} | |
} | |
console.log(quickSort([12, 5, 2, 11, 8, 6, 7])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment