Last active
August 21, 2016 08:28
-
-
Save namse/19d1560be298291a359c4b778a06d669 to your computer and use it in GitHub Desktop.
This file contains 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
// from https://gist.github.com/zygygy/0feaff3f25ebe7e7e4dafa7b7f18c061 | |
function partition(seq, pred) { | |
const first = []; | |
const second = []; | |
seq.forEach(i => { | |
if (pred(i)) { | |
first.push(i); | |
} else { | |
second.push(i); | |
} | |
}); | |
return { | |
first, | |
second, | |
} | |
} | |
function qsort(con) { | |
if (!!!con || !!!con.length) { | |
return []; | |
} | |
const head = con[0]; | |
const tail = con.slice(1); | |
const comp = (v) => v < head; | |
const { | |
first: low, | |
second: high, | |
} = partition(tail, comp); | |
return [].concat( | |
qsort(low), | |
[head], | |
qsort(high) | |
); | |
} | |
console.log(qsort([5,3,6,1,5,67,7,2])); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment