04.02.2018 Views

Algorithms

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

2.1 ■ Elementary Sorts<br />

257<br />

on your computer to learn the extent to which its conclusion about insertion sort and<br />

selection sort is robust.<br />

Property D is intentionally a bit vague—the value of the small constant factor is left<br />

unstated and the assumption that the costs of compares and exchanges are similar is left<br />

unstated—so that it can apply in a broad variety of situations. When possible, we try to<br />

capture essential aspects of the performance of each of the algorithms that we study in<br />

statements like this. As discussed in Chapter 1, each Property that we consider needs to<br />

be tested scientifically in a given situation, perhaps supplemented with a more refined<br />

hypothesis based upon a related Proposition (mathematical truth).<br />

For practical applications, there is one further step, which is crucial: run experiments<br />

to validate the hypothesis on the data at hand. We defer consideration of this step to<br />

Section 2.5 and the exercises. In this case, if your sort keys are not distinct and/or<br />

not randomly ordered, Property D might not hold. You can randomly order an array<br />

with StdRandom.shuffle(), but applications with significant numbers of equal keys<br />

involve more careful analysis.<br />

Our discussions of the analyses of algorithms are intended to be starting points, not<br />

final conclusions. If some other question about performance of the algorithms comes<br />

to mind, you can study it with a tool like SortCompare. Many opportunities to do so<br />

are presented in the exercises.<br />

We do not dwell further on the comparative performance of insertion sort and selection<br />

sort because we are much more interested in algorithms that can run a hundred or<br />

a thousand or a million times faster than either. Still, understanding these elementary<br />

algorithms is worthwhile for several reasons:<br />

■ They help us work out the ground rules.<br />

■ They provide performance benchmarks.<br />

■ They often are the method of choice in some specialized situations.<br />

■ They can serve as the basis for developing better algorithms.<br />

For these reasons, we will use the same basic approach and consider elementary algorithms<br />

for every problem that we study throughout this book, not just sorting. Programs<br />

like SortCompare play a critical role in this incremental approach to algorithm<br />

development. At every step along the way, we can use such a program to help evaluate<br />

whether a new algorithm or an improved version of a known algorithm provides the<br />

performance gains that we expect.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!