Skip to content

Instantly share code, notes, and snippets.

@mjn
mjn / bubblesort.erl
Created May 9, 2012 19:07
Erlang implementation of bubble sort
-module(bubblesort).
-export([sort/1]).
-import(lists, [reverse/1]).
sort(L) ->
sort(L, [], true).
sort([], L, true) ->
reverse(L);
@mjn
mjn / inssort.erl
Created May 9, 2012 19:06
Erlang implementation of insertion sort
-module(inssort).
-export([sort/1]).
sort([]) ->
[];
sort([ First ]) ->
[ First ];
sort([ First, Second | T ]) ->
if
First > Second ->
@mjn
mjn / avltree.rb
Created May 9, 2012 19:04
Erlang implementation of an AVL tree
-module(avltree).
-export([empty_tree/0, insert/3, lookup/2, write_tree/1]).
lookup(Key, { nil, nil, 0, nil, nil }) ->
not_found;
lookup(Key, { Key, Value, _, _, _ }) ->
{ found, Value };
lookup(Key, { Key1, _, _, Smaller, _ }) when Key < Key1 ->
@mjn
mjn / rbtree.erl
Created May 9, 2012 19:03
Erlang implementation of a red-black tree
-module(rbtree).
-export([insert/3, find/2]).
% Node structure: { Key, Value, Color, Smaller, Bigger }
find(_, nil) ->
not_found;
find(Key, { Key, Value, _, _, _ }) ->
{ found, { Key, Value } };