DEV Community

Cover image for Sorting Algorithms with Javascript (Part 2)

Sorting Algorithms with Javascript (Part 2)

Kelvin Wangonya on November 02, 2018

As promised, here's the second part of the post. You can read the first part here. I'm going to show Javascript implementations of three more sort...
Collapse
 
chenge profile image
chenge

quicksort in Ruby. Short and clear.

def qs a
  (pivot = a.pop) ? 
    qs(a.select{|i| i <= pivot}) + [pivot] + qs(a.select{|i| i > pivot}) :
    []
end

Enter fullscreen mode Exit fullscreen mode
Collapse
 
simoroshka profile image
Anna Simoroshka • Edited

I can do pretty much the same in javascript. :)

const qs = (a) => {
   const pivot = a.pop();
   return pivot != null ? [...qs(a.filter(i => i < pivot)), pivot, ...qs(a.filter(i => i >= pivot))] : [];
}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
wangonya profile image
Kelvin Wangonya

Wow! I've never written a line of ruby in my life 😅 This is really clean.