20 Analysis of Selection Sort
20 Analysis of Selection Sort
Selection sort loops over indices in the array; for each index, selection sort
calls indexOfMinimum and swap. If the length of the array is n, there are n
indices in the array.
Since each execution of the body of the loop runs two lines of code, you might
think that 2n lines of code are executed by selection sort. But it's not true!
Remember that indexOfMinimum and swap are functions: when either is
called, some lines of code are executed.
How many lines of code are executed by a single call to swap? In the usual
implementation, it's three lines, so that each call to swap takes constant time.
3. In the third call, it looks at the subarray from indices 2 to 7; the loop body
runs 6 times.
4. In the fourth call, the subarray from indices 3 to 7; the loop body runs 5
times.
5. …
6. In the eighth and final call of indexOfMimimum, the loop body runs just
1 time.
(8+1)+(7+2)+(6+3)+(5+4)=9+9+9+9
=4 * 9
=36
There were four pairs of numbers, each of which added up to 9. So here's the
general trick to sum up any sequence of consecutive integers:
What if the number of integers in the sequence is odd, so that you cannot pair
them all up? It doesn't matter! Just count the unpaired number in the middle
of the sequence as half a pair. For example, let's sum up 1 + 2 + 3 + 4 + 5. We
have two full pairs (1 + 5 and 2 + 4, each summing to 6) and one "half pair" (3,
which is half of 6), giving a total of 2.5 pairs. We multiply 2.5 * 6 = 15, and we
get the right answer.
The total running time for selection sort has three parts:
3. The running time for the rest of the loop in the selectionSort function.
Parts 2 and 3 are easy. We know that there are n calls to swap, and each call
takes constant time. Using our asymptotic notation, the time for all calls to
swap is Θ(n). The rest of the loop in selectionSort is really just testing and
incrementing the loop variable and calling indexOfMinimum and swap, and
so that takes constant time for each of the nn iterations, for another Θ(n)
time.
Adding up the running times for the three parts, we have Θ(n2) for the calls to
indexOfMinimum, Θ(n) for the calls to swap, and Θ(n) for the rest of the loop
Let's see how the Θ(n2) running time affects the actual execution time. Let's
say that selection sort takes approximately n2/106 seconds to sort n values.
Let's start with a fairly small value of n, let's say n = 100. Then the running
time of selection sort is about 1002/106 = 1/100 seconds. That seems pretty fast.
But what if n = 1000? Then selection sort takes about 10002/106 =1 second. The
array grew by a factor of 10, but the running time increased 100 times. What