Skip to content

Instantly share code, notes, and snippets.

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