Various Quicksort Implementations in Elixir

Elixir has built-in sort functions Enum.sort/1 and Enum.sort/2. For list type, it calls Erlang :lists.sort/1 and :lists.sort/2 that use merge sort algorithm. For other Elixir types that implement Enumerable protocol, Elixir implements its own merge sort. Here’s the link to the source code.

Since Elixir list is implemented as singly linked list, merge sort is the obvious choice for sorting. But I’ve never implemented quicksort with a linked list before, so I decided to give it a try with Elixir.


There’s little practical value in implementing quicksort with linked list. Just use the merge sort implementation provided by Elixir if you need sorting.

Naive implementation

There is an Erlang example provided by Joe Armstrong himself in Programming Erlang. Here is its Elixir equivalent.

It’s a succint implementation that does its job. Unfortunately, it uses the head of the list as its pivot, which would always lead to the worst case performance of quicksort O(n^2) for already sorted lists.

Random pivot implementation

This problem can be mitigated by taking a random element in the list as the pivot.

Not so succint this time. The problems inherent in linked list start to appear.

In order to pick a random index, random_position_in/1 has to call Kernel.length/1, which has to traverse the entire list once and adds a constant to the O(nlogn) performance.

List.pop_at/3 pops the pivot element at the given index and returns a tuple that contains the pivot element and the rest of the list. It also traverses the list until it reaches the given index.

I could have written an alternative implementation by using Enum.random/1 to immediately get a random element from the list instead of an index and use it as the pivot, but that would have required me to deal with elements identical to the randomly chosen pivot element. I decided to take a seemingly simpler route. Enum.random/1 has the same performance issue because it traverses the list until it reaches the randomly chosen index, anyway.

Median pivot implementation

Quicksort is most efficient when it uses the true median of the list as its pivot, but it’s costly to calculate the true median. Sedgewick recommends using a median of three elements of the list as a pivot for a highly efficient approximation of the true median. This enables better and more stable performance than when using a random pivot.

This is almost identical to the previous solution, except that I defined index_of_median_of_three_random_elements_from/1. It uses Enum.take_random/2, which traverses the given list to randomly pick three elements from it. Sedgewick recommends simply using first, last, and middle elements of the array, but I think random choice would be good enough.

Dual pivot implementation

First proposed by Yaroslavskiy’s paper in 2009, dual pivot quicksort has been found to be faster than single pivot quicksort. Its carefully optimized variant is implemented in Java 7 Array class.

Whereas single pivot quicksort gets more efficient the closer the pivot is to the true median, that strategy is suboptimal for dual pivot quicksort. My unverified intuition is that dual pivot quicksort operates most efficiently when each pivot is located around 33% and 66% percentile of the list.

Yaroslavskiy’s sample pseudocode uses first and last elements as the pivots, but that would also lead to O(n^2) worse case performance for already sorted lists.

I decided to use random pivots for a hopefully more stable performance, although I didn’t benchmark it.