Online Book Reader

Home Category

Beautiful Code [97]

By Root 5256 0
I Never Wrote > The Most Beautiful Code I Ever Wrote

3. The Most Beautiful Code I Never Wrote

Jon Bentley

I once heard a master programmer praised with the phrase, "He adds function by deleting code." Antoine de Saint-Exupéry, the French writer and aviator, expressed this sentiment more generally when he said, "A designer knows he has achieved perfection not when there is nothing left to add, but when there is nothing left to take away." In software, the most beautiful code, the most beautiful functions, and the most beautiful programs are sometimes not there at all.

It is, of course, difficult to talk about things that aren't there. This chapter attempts this daunting task by presenting a novel analysis of the runtime of the classic Quicksort program. The first section sets the stage by reviewing Quicksort from a personal perspective. The subsequent section is the meat of this chapter. We'll start by adding one counter to the program, then manipulate the code to make it smaller and smaller and yet more and more powerful until just a few lines of code completely capture its average runtime. The third section summarizes the techniques, and presents a particularly succinct analysis of the cost of binary search trees. The final two sections draw insights from the chapter to help you write more elegant programs.

3.1. The Most Beautiful Code I Ever Wrote

When Greg Wilson first described the idea of this book, I asked myself what was the most beautiful code I had ever written. After this delicious question rolled around my brain for the better part of a day, I realized that the answer was easy: Quicksort. Unfortunately, the one question has three different answers, depending on precisely how it is phrased.

I wrote my thesis on divide-and-conquer algorithms, and found that C.A.R. Hoare's Quicksort ("Quicksort," Computer Journal 5) is undeniably the granddaddy of them all. It is a beautiful algorithm for a fundamental problem that can be implemented in elegant code. I loved the algorithm, but I always tiptoed around its innermost loop. I once spent two days debugging a complex program that was based on that loop, and for years I carefully copied that code whenever I needed to perform a similar task. It solved my problems, but I didn't really understand it.

I eventually learned an elegant partitioning scheme from Nico Lomuto, and was finally able to write a Quicksort that I could understand and even prove correct. William Strunk Jr.'s observation that "vigorous writing is concise" applies to code as well as to English, so I followed his admonition to "omit needless words" (The Elements of Style). I finally reduced approximately 40 lines of code to an even dozen. So if the question is, "What is the most beautiful small piece of code that you've ever written?" my answer is the Quicksort from my book Programming Pearls, Second Edition (Addison-Wesley). This Quicksort function, implemented in C, is shown in Example 3-1. We'll further study and refine this example in the next section.

Example 3-1. Quicksort function

void quicksort(int l, int u)

{ int i, m;

if (l >= u) return;

swap(l, randint(l, u));

m = l;

for (i = l+1; i <= u; i++)

if (x[i] < x[l])

swap(++m, i);

swap(l, m);

quicksort(l, m-1);

quicksort(m+1, u);

}

This code sorts a global array x[n] when called with the arguments quicksort(0, n-1). The two parameters of the function are the indexes of the subarray to be sorted: l for lower and u for upper. The call swap(i, j) exchanges the contents of x[i] and x[j]. The first swap randomly chooses a partitioning element uniformly selected between l and u.

Programming Pearls contains a detailed derivation and proof of correctness for the quicksort function. Throughout the rest of this chapter, I'll assume that the reader is familiar with Quicksort to the level presented in that description and in most elementary algorithms textbooks.

If you change the question to, "What is the most beautiful piece of code that you've written that was widely used?" my answer is again a Quicksort. An article I wrote with

Return Main Page Previous Page Next Page

®Online Book Reader