Skip to content

Instantly share code, notes, and snippets.

@lucaschain
Last active April 29, 2020 16:25
Show Gist options
  • Save lucaschain/5508f16ec581fbc8d413608fb89ba74b to your computer and use it in GitHub Desktop.
Save lucaschain/5508f16ec581fbc8d413608fb89ba74b to your computer and use it in GitHub Desktop.
Functional Javascript Quick Sort
const filter = (list, predicate) => list.filter(predicate)
const filterLessThan = (list, value) => filter(list, item => item < value)
const filterGreaterOrEqualThan = (list, value) => filter(list, item => item >= value)
const quickSort = ([head, ...tail]) => head === undefined ? [] : [
...quickSort(filterLessThan(tail, head)),
head,
...quickSort(filterGreaterOrEqualThan(tail, head))
]
const randomInteger = (min, max) => parseInt(min + (Math.random() * (max - min + 1)))
const randomIntegerList = (size, min, max) => [...new Array(size)].map(randomInteger.bind(null, min, max))
console.log(quickSort(randomIntegerList (100, 1, 1000)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment