Last active
February 15, 2017 14:32
-
-
Save tiagopog/ff4ce373ffd6c719033e805f92da82ab to your computer and use it in GitHub Desktop.
Elixir - Quicksort sample
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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