Quicksort the Ruby Way
I’m gearing up to apply for my next engineering job by reviewing some classic algorithms and implementing them in Ruby.
Quicksort is a recursive algorithm for sorting lists of values. I won’t recapitulate the details of how quicksort works here. This explanation from Khan Academy is good. I used the JavaScript implementation developed in that course as the basis for my own.
This post is for folks who write Ruby and want to see what an idiomatic Ruby implementation of quicksort might look like. In particular, I’ve eschewed the overly terse single-letter-variable salad you so often find in example implementations of basic algorithms.
Here’s the code, followed by some basic RSpec unit tests.
Quicksort Implementation
Quicksort Unit Tests