読者です 読者をやめる 読者になる 読者になる

クイックソート、リバース

#!/usr/bin/env escript
print_list(List) ->
  io:format("len: ~p~n", [length(List)]),
  lists:foreach(fun(X) -> io:format("~p, ",[X]) end, List),
  io:format("~n").

quick_sort([]) -> [];
quick_sort([Pivot|Rest]) ->
  {Smaller, Larger} = partition(Pivot, Rest, [], []),
  quick_sort(Smaller) ++ [Pivot] ++ quick_sort(Larger).

partition(_, [], Smaller, Larger) -> {Smaller, Larger};
partition(Pivot, [H|T], Smaller, Larger) ->
  if H =< Pivot -> partition(Pivot, T, [H|Smaller], Larger);
     H >  Pivot -> partition(Pivot, T, Smaller, [H|Larger])
  end.

main(_) ->
  List = [73, 74, 72, 75, 71],
  print_list(quick_sort(List)).
#!/usr/bin/env escript
print_list(List) ->
  io:format("len: ~p~n", [length(List)]),
  lists:foreach(fun(X) -> io:format("~p, ",[X]) end, List),
  io:format("~n").

reverse([]) -> [];
reverse([H|T]) ->
  reverse(T) ++ [H].

main(_) ->
  List = [73, 74, 72, 75, 71],
  print_list(reverse(List)).