Java Reference
In-Depth Information
23
24 private static Random generator = new
Random();
25 }
Typical Output
[65, 46, 14, 52, 38, 2, 96, 39, 14, 33, 13, 4, 24,
99, 89, 77, 73, 87, 36, 81]
[2, 4, 13, 14, 14, 24, 33, 36, 38, 39, 46, 52, 65,
73, 77, 81, 87, 89, 96, 99]
S ELF C HECK
1. Why do we need the temp variable in the swap method? What would
happen if you simply assigned a[i] to a[j] and a[j] to a[i] ?
2. What steps does the selection sort algorithm go through to sort the
sequence 6 5 4 3 2 1?
14.2 Profiling the Selection Sort Algorithm
To measure the performance of a program, you could simply run it and measure how
long it takes by using a stopwatch. However, most of our programs run very quickly,
and it is not easy to time them accurately in this way. Furthermore, when a program
takes a noticeable time to run, a certain amount of that time may simply be used for
loading the program from disk into memory (for which we should not penalize it) or
for screen output (whose speed depends on the computer model, even for computers
with identical CPUs). We will instead create a StopWatch class.
631
632
This class works like a real stopwatch. You can start it, stop it, and read out the
elapsed time. The class uses the System.currentTimeMillis method, which
returns the milliseconds that have elapsed since midnight at the start of January 1,
1970. Of course, you don't care about the absolute number of seconds since this
historical moment, but the difference of two such counts gives us the number of
milliseconds of a time interval. Here is the code for the StopWatch class:
Search WWH ::




Custom Search