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
4 - A selection problem
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
Let X and Y be two finite disjoint sets of elements over some ordered type and of combined size greater than k. Consider the problem of computing the kth smallest element of X ⋃ Y. By definition, the kth smallest element of a set is one for which there are exactly k elements smaller than it, so the zeroth smallest is the smallest. How long does such a computation take?
The answer depends, of course, on how the sets X and Y are represented. If they are both given as sorted lists, then O(∣X∣+ ∣Y∣) steps are sufficient. The two lists can be merged in linear time and the kth smallest can be found at position k in the merged list in a further O(k) steps. In fact, the total time is O(k) steps, since only the first k + 1 elements of the merged list need be computed. But if the two sets are given as sorted arrays, then – as we show below – the time can further be reduced to O(log ∣X∣+log∣Y∣) steps. This bound depends on arrays having a constant-time access function. The same bound is attainable if both X and Y are represented by balanced binary search trees, despite the fact that two such trees cannot be merged in less than linear time.
The fast algorithm is another example of divide and conquer, and the proof that it works hinges on a particular relationship between merging and selection.
- Type
- Chapter
- Information
- Pearls of Functional Algorithm Design , pp. 21 - 26Publisher: Cambridge University PressPrint publication year: 2010