Sorting algorithms/Stooge sort: Difference between revisions
Content deleted Content added
m →{{header|REXX}}: removed the use of a literal for writing output. |
Added Elixir |
||
Line 222: | Line 222: | ||
return 0; |
return 0; |
||
}</lang> |
}</lang> |
||
=={{header|C++}}== |
=={{header|C++}}== |
||
Line 403: | Line 402: | ||
{{out}} |
{{out}} |
||
[-6, -5, -2, 1, 3, 3, 4, 5, 7, 10] |
[-6, -5, -2, 1, 3, 3, 4, 5, 7, 10] |
||
=={{header|Eiffel}}== |
=={{header|Eiffel}}== |
||
<lang Eiffel> |
<lang Eiffel> |
||
Line 476: | Line 476: | ||
sorted: |
sorted: |
||
-2 0 2 5 7 66 |
-2 0 2 5 7 66 |
||
</pre> |
|||
=={{header|Elixir}}== |
|||
<lang elixir>defmodule Sort do |
|||
def stooge_sort(list) do |
|||
stooge_sort(List.to_tuple(list), 0, length(list)-1) |> Tuple.to_list |
|||
end |
|||
defp stooge_sort(tuple, i, j) do |
|||
if (vj = elem(tuple, j)) < (vi = elem(tuple, i)) do |
|||
tuple = put_elem(tuple,i,vj) |> put_elem(j,vi) |
|||
end |
|||
if j - i > 1 do |
|||
t = div(j - i + 1, 3) |
|||
tuple |
|||
|> stooge_sort(i, j-t) |
|||
|> stooge_sort(i+t, j) |
|||
|> stooge_sort(i, j-t) |
|||
else |
|||
tuple |
|||
end |
|||
end |
|||
end |
|||
(for _ <- 1..20, do: :rand.uniform(20)) |> IO.inspect |
|||
|> Sort.stooge_sort |> IO.inspect</lang> |
|||
{{out}} |
|||
<pre> |
|||
[18, 8, 19, 19, 17, 17, 1, 5, 17, 9, 13, 6, 7, 19, 1, 6, 11, 20, 17, 12] |
|||
[1, 1, 5, 6, 6, 7, 8, 9, 11, 12, 13, 17, 17, 17, 17, 18, 19, 19, 19, 20] |
|||
</pre> |
</pre> |
||
Line 792: | Line 823: | ||
-9 -6 1 4</lang> |
-9 -6 1 4</lang> |
||
=={{header|MAXScript}}== |
=={{header|MAXScript}}== |
||
<lang MAXScript>fn stoogeSort arr i: j: = |
<lang MAXScript>fn stoogeSort arr i: j: = |
||
Line 1,399: | Line 1,431: | ||
Stop_Data: |
Stop_Data: |
||
EndDataSection</lang> |
EndDataSection</lang> |
||
=={{header|Python}}== |
=={{header|Python}}== |
||
<lang python>>>> data = [1, 4, 5, 3, -6, 3, 7, 10, -2, -5, 7, 5, 9, -3, 7] |
<lang python>>>> data = [1, 4, 5, 3, -6, 3, 7, 10, -2, -5, 7, 5, 9, -3, 7] |
||
Line 1,669: | Line 1,702: | ||
test stoogeSort. |
test stoogeSort. |
||
test printNl.</lang> |
test printNl.</lang> |
||
=={{header|Swift}}== |
=={{header|Swift}}== |
||
<lang Swift>func stoogeSort(inout arr:[Int], _ i:Int = 0, var _ j:Int = -1) { |
<lang Swift>func stoogeSort(inout arr:[Int], _ i:Int = 0, var _ j:Int = -1) { |
||
Line 1,778: | Line 1,812: | ||
PRINT |
PRINT |
||
RETURN</lang> |
RETURN</lang> |
||
=={{header|XPL0}}== |
=={{header|XPL0}}== |
||
<lang XPL0>code ChOut=8, IntOut=11; \intrinsic routines |
<lang XPL0>code ChOut=8, IntOut=11; \intrinsic routines |