Book contents
- Frontmatter
- Contents
- Preface
- 1 The smallest free number
- 2 A surpassing problem
- 3 Improving on saddleback search
- 4 A selection problem
- 5 Sorting pairwise sums
- 6 Making a century
- 7 Building a tree with minimum height
- 8 Unravelling greedy algorithms
- 9 Finding celebrities
- 10 Removing duplicates
- 11 Not the maximum segment sum
- 12 Ranking suffixes
- 13 The Burrows–Wheeler transform
- 14 The last tail
- 15 All the common prefixes
- 16 The Boyer–Moore algorithm
- 17 The Knuth–Morris–Pratt algorithm
- 18 Planning solves the Rush Hour problem
- 19 A simple Sudoku solver
- 20 The Countdown problem
- 21 Hylomorphisms and nexuses
- 22 Three ways of computing determinants
- 23 Inside the convex hull
- 24 Rational arithmetic coding
- 25 Integer arithmetic coding
- 26 The Schorr–Waite algorithm
- 27 Orderly insertion
- 28 Loopless functional algorithms
- 29 The Johnson–Trotter algorithm
- 30 Spider spinning for dummies
- Index
12 - Ranking suffixes
Published online by Cambridge University Press: 05 March 2013
- Frontmatter
- Contents
- Preface
- 1 The smallest free number
- 2 A surpassing problem
- 3 Improving on saddleback search
- 4 A selection problem
- 5 Sorting pairwise sums
- 6 Making a century
- 7 Building a tree with minimum height
- 8 Unravelling greedy algorithms
- 9 Finding celebrities
- 10 Removing duplicates
- 11 Not the maximum segment sum
- 12 Ranking suffixes
- 13 The Burrows–Wheeler transform
- 14 The last tail
- 15 All the common prefixes
- 16 The Boyer–Moore algorithm
- 17 The Knuth–Morris–Pratt algorithm
- 18 Planning solves the Rush Hour problem
- 19 A simple Sudoku solver
- 20 The Countdown problem
- 21 Hylomorphisms and nexuses
- 22 Three ways of computing determinants
- 23 Inside the convex hull
- 24 Rational arithmetic coding
- 25 Integer arithmetic coding
- 26 The Schorr–Waite algorithm
- 27 Orderly insertion
- 28 Loopless functional algorithms
- 29 The Johnson–Trotter algorithm
- 30 Spider spinning for dummies
- Index
Summary
Introduction
The idea of ranking the elements of a list crops up frequently. An element x is assigned rank r if there are exactly r elements of the list less than x. For example, rank [51, 38, 29, 51, 63, 38] = [3, 1, 0, 3, 5, 1]. This scheme ranks from 0 and from lowest to highest, but one can also rank from 1 and from highest to lowest, as when ranking candidates by their marks in an examination. Rankings are distinct if and only if the list does not contain duplicates, in which case rank xs is a permutation of [0 .. length xs − 1].
In this pearl we consider the problem of ranking the suffixes of a list rather than the list itself. It takes Θ(n log n) steps to rank a list of length n, assuming a test x < y takes constant time. Since in the worst case it takes Θ(n) such tests to make one lexicographic comparison between two suffixes of a list of length n, it seems that ranking the suffixes of a list should require Θ(n2 log n) basic comparisons. The point of this pearl is to show that only Θ(n log n) steps are necessary. Asymptotically speaking, it takes no more time to rank the suffixes of a list than it does to rank the list itself. Surprising but true.
- Type
- Chapter
- Information
- Pearls of Functional Algorithm Design , pp. 79 - 90Publisher: Cambridge University PressPrint publication year: 2010