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]
Written by Eduardo
Related protips
1 Response
Since Enum.partition has been deprecated, it should look like this now:
Enum.split_with(rest, &(&1 < pivot))
over 1 year ago
·
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Erlang
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#