Selection Sort Analysis
The number of iterations of the loop 3–7 depends on the value of $j$: it is $\mathit{limit} - j$. Therefore the running time of lines 3–7 is $O(\mathit{limit} - j)$. Lines 8–10 run in $O(1)$ time, so the body of the loop 1–11 runs in time $O(\mathit{limit}-j) + O(1) = O(\mathit{limit}-j)$ (using $j &lt; \mathit{limit}$). Finally, the total running time is
$$
O\left(\sum_{j=0}^{\mathit{limit}-1} \mathit{limit}-j\right) =
O\left(\sum_{j=1}^{\mathit{limit}} j\right) =
O(\mathit{limit}^2),
$$
using the formula $\sum_{j=1}^n j = \frac{n(n+1)}{2}$.