Created
October 8, 2017 15:50
-
-
Save ZhihaoLau/76ac03df341ca6917920a0d8ca2b2719 to your computer and use it in GitHub Desktop.
quickSort in ES6 favor
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
function quickSort(list) { | |
if (list.length < 2) return list | |
let medIdx = Math.floor(list.length / 2), | |
medItem = list[medIdx], | |
newList = [...list.slice(0, medIdx), ...list.slice(medIdx+1)], | |
left = newList.filter(item => item <= medItem), | |
right = newList.filter(item => item > medItem) | |
return [...quickSort(left), medItem, ...quickSort(right)] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment