Skip to content

Instantly share code, notes, and snippets.

@mjn
Created May 9, 2012 19:07
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mjn/2648077 to your computer and use it in GitHub Desktop.
Save mjn/2648077 to your computer and use it in GitHub Desktop.
Erlang implementation of bubble sort
-module(bubblesort).
-export([sort/1]).
-import(lists, [reverse/1]).
sort(L) ->
sort(L, [], true).
sort([], L, true) ->
reverse(L);
sort([], L, false) ->
sort(reverse(L), [], true);
sort([ X, Y | T ], L, _) when X > Y ->
sort([ X | T ], [ Y | L ], false);
sort([ X | T ], L, Halt) ->
sort(T, [ X | L ], Halt).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment