# Selection Sort in Python

Today we will learn a simple and easy to visualize sorting algorithm called the Selection Sort in Python. Let’s get started.

## The Selection Sort Algorithm

Similar to Insertion Sort, the insertion sort algorithm divides the list into two parts. The first part at the beginning of the list is the sorted part and the second part at the end of the list is unsorted.

Initially, the entire list is unsorted but with every iteration, the smallest item in the list is searched (for ascending list) and added to the sorted section.

How this happens is that one by one, we find the smallest item in the unsorted section and swap it with the item at its correct position.

So in the first iteration, the smallest item is swapped with the item at the first position. In the second iteration, the second smallest item is swapped with the item at the second position. And so on…

## Implementing Selection Sort in Python

Below is a simple implementation of selection sort in Python.

```def selection_sort(lst):
n = len(lst)
for i in range(n - 1):
min = i
for j in range(i + 1, n):
if(lst[j] < lst[min]):
min = j
lst[i], lst[min] = lst[min], lst[i]
```

Notice that the function takes in a list and performs the sorting in-place, it is fairly simple to alter the algorithm to return a sorted list instead.

## Explaining the Steps of the Selection Sort Algorithm

This algorithm sorts the list in increasing order, let us see how it works.

• The variable `n` is the number of items in the list.
• Now, `i` goes from `0` to `n - 2`, which means that `i` points from the first item to the second-last item. The role of `i` is that it will always point to where the next smallest item will go, so we will find the smallest item from `i` to the end of the list, and it will be placed at `i`.
• We are considering the item at `i` to be the smallest one for now, because if we fail to find a smaller element after `i`, then `i` holds the correct item.
• Inside, `j` goes from `i + 1` to `n - 1`, which means that `j` will point to all the items after `i`, and it will be responsible to find the smallest item in that range.
• Now we compare the item at `j` to the smallest item we have found yet, and if the item at `j` is smaller, then it becomes the smallest item we have found yet.
• After the inner loop, we have found the smallest item from `i` to `n - 1`, and it is swapped with the item at `i` so that it goes to its correct position.
• The outer loop will continue to select and place the next smallest items one after the other until the entire list is sorted.

Now we will try to dry-run this algorithm on an example and see how it affects the sequence.

Let’s consider the sequence as 12, 16, 11, 10, 14, 13.
Size of the given list (n): 6

12, 16, 11, 10, 14, 13
10, 16, 11, 12, 14, 13
10, 11, 16, 12, 14, 13
10, 11, 12, 16, 14, 13
10, 11, 12, 13, 14, 16
10, 11, 12, 13, 14, 16

• The numbers in green are the ones that have been sorted.
• The numbers in blue are the smallest ones from the ones that are not sorted.
• The uncolored ones are to be sorted.

It can be seen that the algorithm finds the smallest items and then swaps them with the item at their correct place.

## The Output

Running the same list on the algorithm will produce the following result:

## Conclusion

In this tutorial, we saw how Selection Sort works, we implemented the algorithm in Python and verified the dry-run of an example using the actual output of the code.

Selection Sort, similar to Bubble and Insertion Sort, has a complexity of O(n2). This means that if the input size is doubled, the time it takes to execute the algorithm increases by four times, and so, it is an inefficient sorting algorithm.

It is generally less efficient than Insertion Sort but it is much simpler to understand and implement. I hope you enjoyed learning about Selection Sort and see you in the next tutorial.