Slide 1

Slide 1 text

Heap Sort in Erlang @daqing 2013-6-22

Slide 2

Slide 2 text

Heap Sort 堆排序 2013-6-22

Slide 3

Slide 3 text

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

Slide 4

Slide 4 text

Algorithm • Build a max-heap from the input list • Put root value into result • Swap root node • loop 2013-6-22

Slide 5

Slide 5 text

Represents a Tree in Erlang 2013-6-22

Slide 6

Slide 6 text

Tree Structure {Value, Left, Right} {Value, {Value, Left, Right}, {Value, Left, Right}} 2013-6-22

Slide 7

Slide 7 text

5 1 2 3 4 {5, {4, {1, undefined, undefined}, {2, undefined, undefined} }, {3, undefined, undefined} } Max-heap in Erlang 2013-6-22

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

Zipper http://ferd.ca/yet-another-article-on-zippers.html 2013-6-22

Slide 11

Slide 11 text

{ [ {direction, Value, OtherBranch} | T ], {CurrentVal, CurrentLeft, CurrentRight} } Zipper 2013-6-22

Slide 12

Slide 12 text

Build Max-heap 2013-6-22

Slide 13

Slide 13 text

Swap Root Node 2013-6-22

Slide 14

Slide 14 text

5 1 2 3 4 1 2 3 4 2 4 3 2 1 Swap Root Node 2013-6-22

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

Breadth-first Traversal 2013-6-22

Slide 17

Slide 17 text

2013-6-22

Slide 18

Slide 18 text

5 1 2 3 4 [2, 4, 3, 1] 4 3 2 1 Build Max-heap 2013-6-22

Slide 19

Slide 19 text

Algorithm • Build a max-heap from the input list • Put root value into result • Swap root node • loop 2013-6-22

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

Source Code https://github.com/daqing/algorithms/blob/master/heap_sort/heap_sort.erl 2013-6-22

Slide 22

Slide 22 text

Questions? 2013-6-22

Slide 23

Slide 23 text

Thanks https://github.com/daqing https://github.com/daqing/algorithms @daqing 2013-6-22