Skip to main content

A Duality of Sorts

Ralf Hinze‚ José Pedro Magalhães and Nicolas Wu

Abstract

Sorting algorithms are one of the key pedagogical foundations of computer science, and their properties have been studied heavily. Perhaps less well known, however, is the fact that many of the basic sorting algorithms exist as a pair, and that these pairs arise naturally out of the duality between folds and unfolds. In this paper, we make this duality explicit, by showing how to define common sorting algorithms as folds of unfolds, or, dually, as unfolds of folds. This duality is preserved even when considering optimised sorting algorithms that require more exotic variations of folds and unfolds, and intermediary data structures. While all this material arises naturally from a categorical modelling of these recursion schemes, we endeavour to keep this presentation accessible to those not versed in abstract nonsense.

Affiliation
University of Oxford‚ Computing Laboratory‚ Wolfson Building‚ Parks Road‚ Oxford OX1 3QD‚ England
Book Title
The Beauty of Functional Code
Editor
Achten‚ Peter and Koopman‚ Pieter
ISBN
978−3−642−40354−5
Pages
151−167
Publisher
Springer Berlin Heidelberg
Series
Lecture Notes in Computer Science
Volume
8106
Year
2013