Skip to main content

Sorting with bialgebras and distributive laws

Ralf Hinze‚ Daniel W.H. James‚ Thomas Harper‚ Nicolas Wu and José Pedro Magalhães

Abstract

Sorting algorithms are an intrinsic part of functional programming folklore as they exemplify algorithm design using folds and unfolds. This has given rise to an informal notion of duality among sorting algorithms: insertion sorts are dual to selection sorts. Using bialgebras and distributive laws, we formalise this notion within a categorical setting. We use types as a guiding force in exposing the recursive structure of bubble, insertion, selection, quick, tree, and heap sorts. Moreover, we show how to distill the computational essence of these algorithms down to one-step operations that are expressed as natural transformations. From this vantage point, the duality is clear, and one side of the algorithmic coin will neatly lead us to the other `for free'. As an optimisation, the approach is also extended to paramorphisms and apomorphisms, which allow for more efficient implementations of these algorithms than the corresponding folds and unfolds.

Address
New York‚ NY‚ USA
Book Title
Proceedings of the 8th ACM SIGPLAN workshop on Generic programming
ISBN
978−1−4503−1576−0
Location
Copenhagen‚ Denmark
Pages
69–80
Publisher
ACM
Series
WGP '12
Year
2012