Skip to main content Accessibility help
×
Hostname: page-component-84b7d79bbc-lrf7s Total loading time: 0 Render date: 2024-07-30T09:12:52.172Z Has data issue: false hasContentIssue false

4 - A selection problem

Published online by Cambridge University Press:  05 March 2013

Richard Bird
Affiliation:
University of Oxford
Get access

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 XY. 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
Publisher: Cambridge University Press
Print publication year: 2010

Access options

Get access to the full version of this content by using one of the access options below. (Log in options will check for institutional or personal access. Content may require purchase if you do not have access.)

Save book to Kindle

To save this book to your Kindle, first ensure coreplatform@cambridge.org is added to your Approved Personal Document E-mail List under your Personal Document Settings on the Manage Your Content and Devices page of your Amazon account. Then enter the ‘name’ part of your Kindle email address below. Find out more about saving to your Kindle.

Note you can select to save to either the @free.kindle.com or @kindle.com variations. ‘@free.kindle.com’ emails are free but can only be saved to your device when it is connected to wi-fi. ‘@kindle.com’ emails can be delivered even when you are not connected to wi-fi, but note that service fees apply.

Find out more about the Kindle Personal Document Service.

  • A selection problem
  • Richard Bird, University of Oxford
  • Book: Pearls of Functional Algorithm Design
  • Online publication: 05 March 2013
  • Chapter DOI: https://doi.org/10.1017/CBO9780511763199.005
Available formats
×

Save book to Dropbox

To save content items to your account, please confirm that you agree to abide by our usage policies. If this is the first time you use this feature, you will be asked to authorise Cambridge Core to connect with your account. Find out more about saving content to Dropbox.

  • A selection problem
  • Richard Bird, University of Oxford
  • Book: Pearls of Functional Algorithm Design
  • Online publication: 05 March 2013
  • Chapter DOI: https://doi.org/10.1017/CBO9780511763199.005
Available formats
×

Save book to Google Drive

To save content items to your account, please confirm that you agree to abide by our usage policies. If this is the first time you use this feature, you will be asked to authorise Cambridge Core to connect with your account. Find out more about saving content to Google Drive.

  • A selection problem
  • Richard Bird, University of Oxford
  • Book: Pearls of Functional Algorithm Design
  • Online publication: 05 March 2013
  • Chapter DOI: https://doi.org/10.1017/CBO9780511763199.005
Available formats
×