Skip to content

Instantly share code, notes, and snippets.

@albertywu
Last active May 11, 2016 17:00
Show Gist options
  • Select an option

  • Save albertywu/7117dd22329135747313ab9d105a33dd to your computer and use it in GitHub Desktop.

Select an option

Save albertywu/7117dd22329135747313ab9d105a33dd to your computer and use it in GitHub Desktop.
// quicksort sorting algorithm, in 2 lines of code
let quicksort = ([a, ...as]) => {
if (a == null) return as
return quicksort(as.filter(x => x <= a))
.concat([a])
.concat(quicksort(as.filter(x => x > a)))
}
let test1 = [5,1,2,3,5,6];
let test2 = ['c', 'a', 'm', 'b', 'd', 'p']
let test3 = []
console.log(quicksort(test1)) // [1,2,3,4,5,6]
console.log(quicksort(test2)) // ['a', 'b', 'c', 'd', 'm', 'p']
console.log(quicksort(test3)) // []
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment