Last Updated: November 06, 2019
·
2.504K
· edgurgel

Quicksort in Elixir

defmodule Quick do
  def sort([]), do: []
  def sort([pivot | rest]) do
    { left, right } = Enum.partition(rest, &(&1 < pivot))
    sort(left) ++ [pivot | sort(right)]
  end
end
iex> Quick.sort [5, 4, 3, 2, 1]
[1, 2, 3, 4, 5]

http://elixir-lang.org/

1 Response
Add your response

Since Enum.partition has been deprecated, it should look like this now:
Enum.split_with(rest, &(&1 < pivot))

over 1 year ago ·