August 2020
Intermediate to advanced
508 pages
11h 53m
English
Selection Sort contains two types of steps: comparisons and swaps. That is, we compare each value with the lowest number we’ve encountered in each pass-through, and we swap the lowest number into its correct position.
Looking back at our example array that contains five elements, we had to make a total of 10 comparisons. Let’s break it down in the following table:
Pass-Through # | # of Comparisons |
|---|---|
1 | 4 comparisons |
2 | 3 comparisons |
3 | 2 comparisons |
4 | 1 comparison |
That’s a grand total of 4 + 3 + 2 + 1 = 10 comparisons.
To put it in a way that works for arrays of all sizes, we’d say that for N elements, we make
(N - 1) + (N - 2) + (N - 3) … + 1 comparisons.
As for swaps, though, we only need to make a maximum of one swap per ...
Read now
Unlock full access