# Selection Sort

published

last modified

3kb

Selection sort is a sorting algorithm. It is one of the more inefficient sorting algorithms available. It takes in the worst case $O(n^2)$ in time complexity.

## How Does Selection Sort... Sort?

The idea behind selection sort is very simple. We iterate through our array and find the index of the smallest element. Once we pass through the array we swap the first value with current minimum value.

I like to think of it as a claw machine grabbing the minumum element at every iteration and placing it in $n - i^{ith}$ position.

Now the first position is sorted. We do this for the rest of the $n - i$ elements (because the first element is now sorted). That's it.

It's easy to see why this is $O(n^2)$. For every element $i$ we have to iterate through the array $n - i$ times. Let's say we have an array of $n=5$. Then we have to iterate:

$(n - 1) + (n - 2) + ... + 1$Which by arithmetic progression we know to be:

$\sum\_{i=1}^{n - 1}i = \frac{(n - 1) + 1}{2}(n-1) = \frac{1}{2}n(n-1) = \frac{1}{2}(n^2 - n)$In Big O analysis we can drop the constants and keep the dominant power, which in this case is $n^2$, thus having a $O(n^2)$.

## Implementation

Below is an implementation.

## Contact

If you have any comments or thoughts you can reach me at any of the places below.