June 2018 Update
This commit is contained in:
parent
ba8067c3b7
commit
22f33d4004
5278 changed files with 84726 additions and 14379 deletions
|
|
@ -1,17 +1,11 @@
|
|||
defmodule Sort do
|
||||
def bubble_sort(list) when length(list)<=1, do: list
|
||||
def bubble_sort(list) when is_list(list), do: bubble_sort(list, [])
|
||||
def bsort(list) when is_list(list) do
|
||||
t = bsort_move(list)
|
||||
|
||||
def bubble_sort([x], sorted), do: [x | sorted]
|
||||
def bubble_sort(list, sorted) do
|
||||
{rest, [max]} = Enum.split(bubble_move(list), -1)
|
||||
bubble_sort(rest, [max | sorted])
|
||||
if t == list do t else bsort(t) end
|
||||
end
|
||||
|
||||
def bubble_move([x]), do: [x]
|
||||
def bubble_move([x, y | t]) when x > y, do: [y | bubble_move([x | t])]
|
||||
def bubble_move([x, y | t]) , do: [x | bubble_move([y | t])]
|
||||
|
||||
def bsort_move([x, y | t]) when x > y, do: [y | bsort_move([x | t])]
|
||||
def bsort_move([x, y | t]), do: [x | bsort_move([y | t])]
|
||||
def bsort_move(list), do: list
|
||||
end
|
||||
|
||||
IO.inspect Sort.bubble_sort([3,2,1,4,5,2])
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue