Chapter 8 Searching and Sorting

Version 5.2.11

Handouts: BinarySearch.java.

A. Searching

Two methods for searching arrays.

The Big Idea

Whenever we can reduce a problem's size by halving it instead of subtracting one, we get a big gain: we reduce a factor of N to a factor of log N.

Examples:

B. Sorting

8.3 Insertion Sort

Dance

Insert-sort with Romanian folk dance (YouTube, 4:04)

8.4 The Comparable Interface

8.5 Sorting Linked Lists

Retrospect

Footnotes


  1. Revision history:
    • Version 5.2.1, 2012 Nov 30. Added dance link.
    • Version 5.2, 2012 Oct 9. Minor edit with typographical corrections, filling in of examples and other details.
    • Version 5.1, 2010 Oct 12. Typographical corrections.
    • Version 5, 2010 Oct 10. Converted to markdown.
    • Version 4, 2009 Oct 5. Converted to reStructuredText.
    • Version 3, 2008 Oct 7. Minor edit for clarity and emphasis.
    • Version 2, 2007 Sept 29.
    • Version 1, 2006 Oct 4.
  2. Strictly speaking, this is not a generalization, because int is a primitive type: an int value is not an object. Of course we can substitute an Integer object. But int itself is not Comparable. In contrast, consider the Haskell Ord typeclass, which specifies a method of comparison resulting in a value of LT (less than), EQ (equal), or GT (greater than). The Haskell Int type is an instance of Ord, though it is what we would call a primitive type in Java terms; it's not an object (Haskell does not have objects in the same sense that Java does), but it's certainly not a structured type either.