読者です 読者をやめる 読者になる 読者になる

クイックソート

Elixir
#!/usr/bin/env elixir
defmodule QuickSort do
  def quick_sort([]) do
    []
  end

  def quick_sort([pivot|rest]) do
    {smaller, larger} = partition(pivot, rest, [], [])
    quick_sort(smaller) ++ [pivot] ++ quick_sort(larger)
  end

  defp partition(_, [], smaller, larger) do
    {smaller, larger}
  end

  defp partition(pivot, [h|t], smaller, larger) do
    if h <= pivot do
      partition(pivot, t, [h|smaller], larger)
    else
      partition(pivot, t, smaller, [h|larger])
    end
  end
end

list = [101, 108, 104, 106, 102, 109, 103, 105, 110, 100, 107]
IO.inspect(QuickSort.quick_sort(list), char_lists: :as_lists)