Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Implement Heap Sort in Erlang

Implement Heap Sort in Erlang

This keynote gives a short intro about how to implement the heap sort algorithm in Erlang.

Topics covered:

1. Represent trees in Erlang
2. The Zipper technique
3. Breadth-first traversal

David Zhang

June 22, 2013
Tweet

More Decks by David Zhang

Other Decks in Technology

Transcript

  1. L = [1, 5, 3, 2, 4] 5 1 2

    3 4 S = [ , , , , ] 1 2 3 4 2 5 4 3 2 1 4 2013-6-22
  2. Algorithm • Build a max-heap from the input list •

    Put root value into result • Swap root node • loop 2013-6-22
  3. 5 1 2 3 4 {5, {4, {1, undefined, undefined},

    {2, undefined, undefined} }, {3, undefined, undefined} } Max-heap in Erlang 2013-6-22
  4. L = [1, 5, 3, 2, 4] 5 1 2

    3 4 Build Max-heap 2013-6-22
  5. L = [1, 5, 3, 2, 4] Build Max-heap {5,

    {4, {1, undefined, undefined}, {2, undefined, undefined} }, {3, undefined, undefined} } 2013-6-22
  6. 5 1 2 3 4 1 2 3 4 2

    4 3 2 1 Swap Root Node 2013-6-22
  7. 5 1 2 3 4 [2, 4, 3, 1] 4

    3 2 1 Convert Heap to List 2013-6-22
  8. 5 1 2 3 4 [2, 4, 3, 1] 4

    3 2 1 Build Max-heap 2013-6-22
  9. Algorithm • Build a max-heap from the input list •

    Put root value into result • Swap root node • loop 2013-6-22
  10. 5 1 2 3 4 S = [ , ,

    , , ] 1 2 3 4 2 5 4 3 2 1 4 1 3 2 1 3 2 1 3 1 2 1 2 1 2 1 1 2013-6-22