A Collection of Code Snippets in as Many Programming Languages as Possible
This project is maintained by TheRenegadeCoder
Welcome to the Insertion Sort in Elixir page! Here, you'll find the source code for this program as well as a description of how the program works.
defmodule InsertionSort do
@doc """
Sorts a list of integers in ascending order using insertion sort.
"""
@spec insertion_sort(list :: list(integer()), acc :: list(integer())) :: list(integer())
def insertion_sort(list, acc \\ [])
def insertion_sort([], acc), do: acc
def insertion_sort([first | rest], acc), do: insertion_sort(rest, insert_sorted(acc, first))
@spec insert_sorted(list :: list(integer()), element :: integer()) :: list(integer())
defp insert_sorted(list, element) do
{left, right} = Enum.split_while(list, &(&1 < element))
left ++ [element | right]
end
@spec parse_list(str :: String.t()) :: {:ok, list(integer())} | :error
def parse_list(""), do: :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} <- InsertionSort.parse_list(arg) do
InsertionSort.insertion_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
Insertion Sort in Elixir was written by:
If you see anything you'd like to change or update, please consider contributing.
No 'How to Implement the Solution' section available. Please consider contributing.
No 'How to Run the Solution' section available. Please consider contributing.