In [1]:
#%autosave 0
from IPython.core.display import HTML, display
display(HTML('<style>.container { width:100%; !important } </style>'))
The algorithm selection sort is specified via two equations:
In [2]:
def sort(L):
if L == []:
return []
x = min(L)
return [x] + sort(delete(x, L))
The algorithm to delete an element $x$ from a list $L$ is formulated recursively. There are three cases:
delete
returns the
rest of $L$:
$$ \mathtt{delete}(x, [x] + R) = R$$
In [3]:
def delete(x, L):
if L == []:
assert L != [], f'delete({x}, [])'
if L[0] == x:
return L[1:]
return [L[0]] + delete(x, L[1:])
In [4]:
L = [3, 5, 7, 4, 8, 1, 2, 3, 11, 13, 2]
sort(L)
Out[4]:
In [ ]: