Skip to content

Instantly share code, notes, and snippets.

@tiagopog
Last active February 15, 2017 14:32
Show Gist options
  • Save tiagopog/ff4ce373ffd6c719033e805f92da82ab to your computer and use it in GitHub Desktop.
Save tiagopog/ff4ce373ffd6c719033e805f92da82ab to your computer and use it in GitHub Desktop.
Elixir - Quicksort sample
defmodule Quicksort do
def sort(list) when length(list) < 2, do: list
def sort(list) do
pivot = Enum.random(list)
less = Enum.filter(list, &(&1 < pivot))
greater = list -- (less ++ [pivot])
sort(less) ++ [pivot] ++ sort(greater)
end
end
["cherries", "grapes", "kiwi", "grapes", "avocado", "pineapple", "peach"]
|> Quicksort.sort
|> IO.inspect
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment