Created
December 18, 2018 05:39
-
-
Save franzwong/97beb05aa9d1a03c4db373ab2afa45e3 to your computer and use it in GitHub Desktop.
Quicksort with ramda
This file contains hidden or 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
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