Commit b1a059cd authored by Ilya Prokhorov's avatar Ilya Prokhorov

Bubble sort by erlang

parent bccbf98b
......@@ -5,13 +5,7 @@ startBubbleSort([CurrentHead|Tail]) ->
compareHeads(CurrentHead, Tail, [], [CurrentHead|Tail]).
compareHeads(CurrentHead, [NextHead|Tail], [], [OriginalListHead|OriginalListTail]) ->
io:fwrite("---\n"),
io:format("CurrentHead: ~w~n", [CurrentHead]),
io:format("NextHead: ~w~n", [NextHead]),
OriginalList = lists:append([[OriginalListHead], OriginalListTail]),
if
CurrentHead < NextHead ->
compareHeads(NextHead, Tail, [CurrentHead], OriginalList);
......@@ -20,16 +14,7 @@ compareHeads(CurrentHead, [NextHead|Tail], [], [OriginalListHead|OriginalListTai
end;
compareHeads(CurrentHead, [NextHead|Tail], [OutputListHead|OutputListTail], [OriginalListHead|OriginalListTail]) ->
io:fwrite("---\n"),
io:format("CurrentHead: ~w~n", [CurrentHead]),
io:format("NextHead: ~w~n", [NextHead]),
% OutputList = lists:append([[OutputListHead], OutputListTail, [CurrentHead]]),
% compareHeads(NextHead, Tail, OutputList);
OriginalList = lists:append([[OriginalListHead], OriginalListTail]),
if
CurrentHead < NextHead ->
OutputList = lists:append([[OutputListHead], OutputListTail, [CurrentHead]]),
......@@ -40,17 +25,14 @@ compareHeads(CurrentHead, [NextHead|Tail], [OutputListHead|OutputListTail], [Ori
end;
compareHeads(CurrentHead, [], [OutputListHead|OutputListTail], OriginalList) ->
io:fwrite("---\n"),
OutputList = lists:append([[OutputListHead],OutputListTail, [CurrentHead]]),
io:format("OutputList: ~w~n", [OutputList]),
if
OriginalList == OutputList ->
io:fwrite("End\n");
io:format("OutputList: ~w~n", [OutputList]);
true ->
startBubbleSort(OutputList)
end.
start() ->
UnsortedList = [7,4,44,2,9,10,6,26,1],
UnsortedList = [69,7,4,44,2,9,10,6,26,1],
startBubbleSort(UnsortedList).
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment