How to resolve the algorithm Sorting Algorithms/Circle Sort step by step in the Elixir programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Sorting Algorithms/Circle Sort step by step in the Elixir programming language
Table of Contents
Problem Statement
Sort an array of integers (of any convenient size) into ascending order using Circlesort. In short, compare the first element to the last element, then the second element to the second last element, etc. Then split the array in two and recurse until there is only one single element in the array, like this: Repeat this procedure until quiescence (i.e. until there are no swaps). Show both the initial, unsorted list and the final sorted list. (Intermediate steps during sorting are optional.) Optimizations (like doing 0.5 log2(n) iterations and then continue with an Insertion sort) are optional. Pseudo code:
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Sorting Algorithms/Circle Sort step by step in the Elixir programming language
Source code in the elixir programming language
defmodule Sort do
def circle_sort(data) do
List.to_tuple(data)
|> circle_sort(0, length(data)-1)
|> Tuple.to_list
end
defp circle_sort(data, lo, hi) do
case circle_sort(data, lo, hi, 0) do
{result, 0} -> result
{result, _} -> circle_sort(result, lo, hi)
end
end
defp circle_sort(data, lo, lo, swaps), do: {data, swaps}
defp circle_sort(data, lo, hi, swaps) do
mid = div(lo + hi, 2)
{data, swaps} = do_circle_sort(data, lo, hi, swaps)
{data, swaps} = circle_sort(data, lo, mid, swaps)
circle_sort(data, mid+1, hi, swaps)
end
def do_circle_sort(data, lo, hi, swaps) when lo>=hi do
if lo==hi and elem(data, lo) > elem(data, hi+1),
do: {swap(data, lo, hi+1), swaps+1},
else: {data, swaps}
end
def do_circle_sort(data, lo, hi, swaps) do
if elem(data, lo) > elem(data, hi),
do: do_circle_sort(swap(data, lo, hi), lo+1, hi-1, swaps+1),
else: do_circle_sort(data, lo+1, hi-1, swaps)
end
defp swap(data, i, j) do
vi = elem(data, i)
vj = elem(data, j)
data |> put_elem(i, vj) |> put_elem(j, vi)
end
end
data = [6, 7, 8, 9, 2, 5, 3, 4, 1]
IO.puts "before sort: #{inspect data}"
IO.puts " after sort: #{inspect Sort.circle_sort(data)}"
You may also check:How to resolve the algorithm Yin and yang step by step in the Prolog programming language
You may also check:How to resolve the algorithm Image noise step by step in the Delphi programming language
You may also check:How to resolve the algorithm Input loop step by step in the Rapira programming language
You may also check:How to resolve the algorithm Longest common subsequence step by step in the Ring programming language
You may also check:How to resolve the algorithm Rosetta Code/Count examples step by step in the Go programming language