Skip to content

Instantly share code, notes, and snippets.

@moondef
Created July 18, 2018 03:05
Show Gist options
  • Save moondef/7c733a78d80e7c0c5e82c3baf8826fc2 to your computer and use it in GitHub Desktop.
Save moondef/7c733a78d80e7c0c5e82c3baf8826fc2 to your computer and use it in GitHub Desktop.
quick sort
const arr = [1, 4, 2, 5, 200, 6, 9]
const qsort = (arr) => {
if (arr.length < 2) {
return arr
} else {
const pivot = arr[Math.floor(Math.random() * arr.length)]
const less = arr.filter(value => value < pivot)
const greater = arr.filter(value => value > pivot)
return [...qsort(less), pivot, ...qsort(greater)]
}
}
console.log(qsort(arr))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment