8.13. Sorting EfficiencyΒΆ
To figure out how efficient the Selection Sort algorithm is, we will analyze the worst case. For units of work, we will consider comparisons and swaps - the two key operations in a sort. This video works through how much work there is for a Selection Sort on a list of 4 items:
Animation used by permission of Virginia Tech
If we did the same work for a selection sort on a list of 100 things (
Step |
Comparisons |
Swaps |
---|---|---|
1 |
99 |
1 |
2 |
98 |
1 |
3 |
97 |
1 |
β¦ |
β¦ |
β¦ |
97 |
3 |
1 |
98 |
2 |
1 |
99 |
1 |
1 |
The total swaps would be 99 steps times 1 swap each = 99. Expressed in terms of
The total comparisons would be 99 + 98 + 97 + β¦ + 3 + 2 + 1 or

Since there were
Overall, our work will be given by:
Or:
Since for Big-O purposes, all we care about is the class of the dominant term, in this case
Important
Selection and Insertion Sort are Quadratic -