Skip to content

Instantly share code, notes, and snippets.

@hirenchauhan2
Created February 19, 2018 05:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hirenchauhan2/72f780a7425d76d6cab3d84fad72576c to your computer and use it in GitHub Desktop.
Save hirenchauhan2/72f780a7425d76d6cab3d84fad72576c to your computer and use it in GitHub Desktop.
Quicksort algorithm in Javascript
/***
* 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