The Selection Sort algorithm is implemented here and the time complexity vs. number of elements examined.

The Selection Sort was written in Python and passed to R for plotting.

Helper functions

Defining helper functions.

import random
def sorted_list(n):
    """ Returns a sorted list of n unique numbers """
    list_out = [_i for _i in range(n)]
    return list_out
def random_list(n):
    """ Returns a randomised list of n unique numbers """
    list_out = sorted_list(n)
    random.shuffle(list_out)
    return list_out
def find_smallest(input_list):
    """ Returns the index of the smallest number of an input list """
    smallest = input_list[0]
    smallest_index = 0
    for i in range(len(input_list)):
        if input_list[i] < smallest:
            smallest = input_list[i]
            smallest_index = i
    return smallest_index

Selection Sort

The Selection Sort works by finding the smallest element and adding it to a new list, then the finding next smallest element and adding that to the new list etc, until a new sorted list is created.

def selection_sort(input_list, print_out):
    """ outputs a selection sorted list as well as the steps required """
    steps = 0
    new_list = []
    for i in range(len(input_list)):
        smallest_index = find_smallest(input_list)
        new_list.append(input_list.pop(smallest_index))
        steps += 1
        if print_out:
            print("i is", i)
            print("smallest_index is", smallest_index)
            print("input_list is", input_list)
            print("new_list is", new_list)
            print("")
    return new_list, steps

Using the Selection Sort

The below example steps through the Selection Sort, showing exactly what happens at each step.

test_list = [random.randrange(1, 101) for i in range(5)]
print("Unsorted list is:", test_list)
## ('Unsorted list is:', [32, 61, 75, 77, 47])
print("Index of smallest number is:", find_smallest(test_list))
## ('Index of smallest number is:', 0)
output = selection_sort(test_list, True)
## ('i is', 0)
## ('smallest_index is', 0)
## ('input_list is', [61, 75, 77, 47])
## ('new_list is', [32])
## 
## ('i is', 1)
## ('smallest_index is', 3)
## ('input_list is', [61, 75, 77])
## ('new_list is', [32, 47])
## 
## ('i is', 2)
## ('smallest_index is', 0)
## ('input_list is', [75, 77])
## ('new_list is', [32, 47, 61])
## 
## ('i is', 3)
## ('smallest_index is', 0)
## ('input_list is', [77])
## ('new_list is', [32, 47, 61, 75])
## 
## ('i is', 4)
## ('smallest_index is', 0)
## ('input_list is', [])
## ('new_list is', [32, 47, 61, 75, 77])
print("Selection sorted list is:", output[0])
## ('Selection sorted list is:', [32, 47, 61, 75, 77])
print("Number of steps taken is:", output[1])
## ('Number of steps taken is:', 5)

Steps vs. number of elements

The number of steps required to Selection Sort increases in linear time (see graph below).

# number of steps required vs. length of list
steps = []
for i in range(1, 200):
    my_list = random_list(i)
    steps.append(selection_sort(my_list, False)[1])
    
print(steps)
## [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199]
# passing list from python to R
steps <- py$steps

n = 1:(length(steps))

par(mfrow = c(1,1))
plot(n, steps, main = "Selection Sort", ylab = "Number of steps", xlab = "Number of elements")