Skip to content

Instantly share code, notes, and snippets.

@franzwong
Created December 18, 2018 05:39
Show Gist options
  • Save franzwong/97beb05aa9d1a03c4db373ab2afa45e3 to your computer and use it in GitHub Desktop.
Save franzwong/97beb05aa9d1a03c4db373ab2afa45e3 to your computer and use it in GitHub Desktop.
Quicksort with ramda
const R = require('ramda')
function quicksort(list) {
return R.ifElse(
R.pipe(R.length, R.gt(2)),
R.identity,
list => {
const [pivot, ...rest] = list
return R.pipe(
R.partition(R.gt(pivot)),
R.map(quicksort),
R.insert(1, [pivot]),
R.reduce(R.concat, [])
)(rest)
}
)(list)
}
const list = [ 14, 79, 13, 92, 16, 6, 71, 17, 74, 9]
const sortedList = quicksort(list)
console.log(sortedList)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment