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:
The total comparisons would be 99 + 98 + 97 + β¦ + 3 + 2 + 1 or \((n - 1) + (n - 2) + (n - 3) + \dots
+ 3 + 2 + 1\text{.}\) So what does that equal? There is a trick to find out - pair up the first item and the last, then the second item and next to last and so on. Each pair of items sums to \(n\text{:}\)
Since there were \(n - 1\) numbers to start with, there will be \(\frac{n - 1}{2}\) pairs, each of which adds to \(n\text{.}\) Multiplying the number of pairs by the sum of each pair gives us the total: \(\frac{n - 1}{2} \cdot n
= \frac{n^2 - n}{2} = \frac{n^2}{2} - \frac{n}{2}\text{.}\)
Since for Big-O purposes, all we care about is the class of the dominant term, in this case \(\frac{n^2}{2}\text{,}\) we will say that Selection Sort is \(O(n^2)\) (Quadratic). A similar process can be used to show that so is Insertion Sort.