Skip to content

Instantly share code, notes, and snippets.

@ohaval
Created October 1, 2021 12:29
Show Gist options
  • Save ohaval/d676ef4ac120d7b3e407edfe29f208f7 to your computer and use it in GitHub Desktop.
Save ohaval/d676ef4ac120d7b3e407edfe29f208f7 to your computer and use it in GitHub Desktop.
The Selection sort algorithm simple and readable implementation in Python
from typing import List
def selection_sort(lst: List[int]) -> None:
"""The Selection sort algorithm.
The sorted list is placed in place.
Wikipedia: https://en.wikipedia.org/wiki/Selection_sort
"""
for i in range(len(lst) - 1):
min_index = i
for j in range(i + 1, len(lst)):
if lst[j] < lst[min_index]:
min_index = j
lst.insert(i, lst.pop(min_index))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment