Selection Sort in Elixir

Published on 28 May 2026 (Updated: 28 May 2026)

Welcome to the Selection Sort in Elixir page! Here, you'll find the source code for this program as well as a description of how the program works.

Current Solution

defmodule SelectionSort do
  @doc """
  Sorts a list of integers in ascending order using selection sort.
  """  
  @spec selection_sort(list :: list(integer()), acc :: list(integer())) :: list(integer())
  def selection_sort(list, acc \\ [])
  def selection_sort([], acc), do: acc
  def selection_sort(list, acc) do
    maximum = Enum.max(list)
    selection_sort(List.delete(list, maximum), [maximum | acc])
  end

  @spec parse_list(str :: String.t()) :: {:ok, list(integer())} | :error
  def parse_list(str) do
    str
    |> String.split(",")
    |> Enum.map(&String.trim/1)
    |> Enum.reduce_while({:ok, []}, fn n, {:ok, acc} ->
      case Integer.parse(n) do
        {n, ""} -> {:cont, {:ok, [n | acc]}}
        _       -> {:halt, :error}
      end
    end)
  end
end

with [arg] <- System.argv(),
     {:ok, [_, _ | _] = list} <- SelectionSort.parse_list(arg) do
  SelectionSort.selection_sort(list) |> Enum.join(", ") |> IO.puts()
else
  _ -> IO.puts("Usage: please provide a list of at least two integers to sort in the format \"1, 2, 3, 4, 5\"")
end

Selection Sort in Elixir was written by:

If you see anything you'd like to change or update, please consider contributing.

How to Implement the Solution

No 'How to Implement the Solution' section available. Please consider contributing.

How to Run the Solution

No 'How to Run the Solution' section available. Please consider contributing.