Channels ▼

Andrew Koenig

Dr. Dobb's Bloggers

More Thoughts On Sorting

June 01, 2011

I have received several comments from readers of this article. The most thoughtful of these was sent to me privately rather than appearing in the comments section, so I will leave it to the author to identify himself if he wishes. I intend the following remarks to address his comments along with the online ones.

First, I failed to make clear an important distinction between the behavior of specific algorithms and the requirements that the C++ Standard imposes on its library. The standard C++ library offers two generic sorting algorithms, sort and stable_sort, and imposes performance requirements on them that suggest, but do not require, particular implementations.

The sort function's requirements are loose: Sorting an N-element sequence requires "approximately N log N" comparisons on the average. For stable_sort, the requirements are both more generous and more rigid: "at most N (logN)2 comparisons; if enough extra memory is available, it is N log N." There is also a footnote that tells readers to use stable_sort if worst-case behavior is important.

These requirements are meant to allow implementations to use Quicksort (which is not stable) for the sort function, and to require an algorithm that is much faster than bubble sort (though usually not as fast as Quicksort), such as an in-place merge sort , for stable_sort. Although it is certainly true that nothing forbids a stable sorting algorithm from running quadratically, such an algorithm is unacceptable in a standard-conforming C++ implementation.

Another reader argued, correctly, that quadratic algorithms are fine for "'last-mile' cases where only a few elements need to be sorted." I completely agree. But we're talking here about what techniques to teach to beginners. When I said that bubble sorts should not appear in a textbook, I intended by context to mean an introductory textbook. Of course bubble sorts should appear in, for example, a textbook that includes a survey of sorting algorithms.

This brings me to the comments that the reader sent me directly. He made what I think are four good points, and left out what I think is a fifth point.

He begins by grouping bubble sort with selection and insertion sort, a grouping with which I agree. All of these algorithms are similar in implementation and have similarly wretched performance. He goes on to argue:

  1. Quadratic sorts are a good example of algorithmic thinking. In particular, they provide a good example for students who have trouble understanding nested loops.
  2. Implementing a quadratic sort provides a good occasion to introduce the notion of measuring performance.
  3. Measuring performance, in turn, makes it possible to drive home the idea of how slow quadratic algorithms really are.
  4. This dramatically bad performance motivates a search for better algorithms.

This reader argues that teaching quadratic sorting algorithms is a good idea for students who wish to become computer scientists, rather than just computer programmers.

I think that all of these arguments make sense, and may ultimately boil down to a matter of taste. For me, these arguments are outweighed by:

  1. Students tend to use the first technique they learn long after they should have outgrown it.

For this reason, I cannot bring myself to teach students a bad way of solving a problem until after I have shown them a good way to solve it — not even if I emphasize that what I am teaching is a bad solution. I am too concerned that I will give them bad habits that will be hard to break later.

That said, I am much less unwilling to teach a bad solution followed by a good one than I am to teach a bad solution by itself.

Of course, for most people — especially those who are acting as programmers rather than as computer scientists — the best way to sort a sequence is to use the standard library. All too many textbooks make four mistakes at once:

  1. They teach readers how to write their own sorting algorithms before using the standard library.
  2. The first sort algorithm they teach has quadratic performance.
  3. They never teach how to write a sort algorithm that performs better; and
  4. They never get around to explaining what a stable sort is or why it is useful.

There is an alternative approach that avoids all four of these problems. I'll explain that strategy in my next article.

Related Reading

More Insights

Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.