I am learning Python, and taking several online courses on computer science and algorithms. One of my assigments is to write a **Selection Sort** in Python. Selection sort is one of the more intuitive sort routines, because it logically works how one would probably sort a small list of items manually.

Selection Sort scans the list and picks the smallest item and puts it at the beginning of the list. It then scans for the next smallest item and puts it next. It continues this same process until every item in the list is sorted.

Using asymptotic notation, Selection Sort is O(n^{2}). If you're looking for something faster, check out my article on Merge Sort.

## Selection Sort

There are lots of different coding examples of Selection Sort using Python. Below is my code, which uses nested `for`

loops.

I also include some unit tests. I plan to include Doctests or unit tests for all my examples, because I want the practice. It also helps me refactor the code if I discover a bug or better implementation.

import unittest def selection_sort(integers): """Performs in-place Selection Sort on a list of integers.""" length = len(integers) if length < 2: return for i in range(length - 1): # set initial position as min val, index minimal_value = integers[i] minimal_index = i for j in range(i + 1, length): if integers[j] < minimal_value: minimal_value = integers[j] minimal_index = j integers[i], integers[minimal_index] \ = integers[minimal_index], integers[i] class selection_sort_tests(unittest.TestCase): def sort(self, lst): copy = lst[:] selection_sort(copy) return copy def test_empty_list(self): lst = [] sorted_lst = self.sort(lst) self.assertEqual(lst, sorted_lst) def test_single_item(self): lst = [1] sorted_lst = self.sort(lst) self.assertEqual(lst, sorted_lst) def test_two_items_sorted(self): lst = [1, 2] sorted_lst = self.sort(lst) self.assertEqual(lst, sorted_lst) def test_two_items_unsorted(self): lst = [2, 1] sorted_lst = self.sort(lst) self.assertEqual(sorted_lst, [1, 2]) def test_zero_in_list(self): lst = [10, 0] sorted_lst = self.sort(lst) self.assertEqual(sorted_lst, [0, 10]) def test_odd_number_of_items(self): lst = [13, 7, 5] sorted_lst = self.sort(lst) self.assertEqual(sorted_lst, [5, 7, 13]) def test_even_number_of_items(self): lst = [23, 7, 13, 5] sorted_lst = self.sort(lst) self.assertEqual(sorted_lst, [5, 7, 13, 23]) def test_duplicate_integers_in_list(self): lst = [1, 2, 2, 1, 0, 0, 15, 15] sorted_lst = self.sort(lst) self.assertEqual(sorted_lst, [0, 0, 1, 1, 2, 2, 15, 15]) def test_larger_integers(self): lst = [135604, 1000000, 45, 78435, 456219832, 2, 546] sorted_lst = self.sort(lst) self.assertEqual(sorted_lst, [2, 45, 546, 78435, 135604, 1000000, 456219832]) if __name__ == '__main__': unittest.main()

As I mentioned on Twitter, I wrote this using **Pythonista** on my iPad Pro. It's nice to get away from the office and complete my assignments on my iPad without having to bring the MacBook Pro.

Pythonista is a great app for developing Python scripts on the iPad Pro.

I hope you find the article useful.

I'll be writing tutorials daily and many of them will be on my homework assignments or personal experiments as I learn Python, data structures, and algorithms. If you want to chat, catch me on Twitter.

**Posted by David Hayden**