Skip to content
Open
9 changes: 5 additions & 4 deletions sorts/insertion_sort.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
python3 insertion_sort.py
"""

from __future__ import annotations

from collections.abc import MutableSequence
from typing import Any, Protocol, TypeVar

Expand Down Expand Up @@ -48,6 +50,9 @@ def insertion_sort[T: Comparable](collection: MutableSequence[T]) -> MutableSequ
>>> collection = random.choices(string.ascii_letters + string.digits, k=100)
>>> insertion_sort(collection) == sorted(collection)
True

Time Complexity: (O(n^2))
Space Complexity:(O(n))
"""

for insert_index in range(1, len(collection)):
Expand All @@ -63,7 +68,3 @@ def insertion_sort[T: Comparable](collection: MutableSequence[T]) -> MutableSequ
from doctest import testmod

testmod()

user_input = input("Enter numbers separated by a comma:\n").strip()
unsorted = [int(item) for item in user_input.split(",")]
print(f"{insertion_sort(unsorted) = }")
22 changes: 16 additions & 6 deletions sorts/pancake_sort.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,28 @@
python pancake_sort.py
"""

from __future__ import annotations

def pancake_sort(arr):
from collections.abc import MutableSequence


def pancake_sort(arr: MutableSequence[int]) -> MutableSequence[int]:
"""Sort Array with Pancake Sort.
:param arr: Collection containing comparable items
:return: Collection ordered in ascending order of items
:param arr: Mutable sequence containing comparable items.
:return: The same sequence sorted in ascending order.
Examples:
>>> pancake_sort([0, 5, 3, 2, 2])
[0, 2, 2, 3, 5]
>>> pancake_sort([])
[]
>>> pancake_sort([-2, -5, -45])
[-45, -5, -2]

Time Complexity:
O(n^2)

Space Complexity:
O(n)
"""
cur = len(arr)
while cur > 1:
Expand All @@ -34,6 +44,6 @@ def pancake_sort(arr):


if __name__ == "__main__":
user_input = input("Enter numbers separated by a comma:\n").strip()
unsorted = [int(item) for item in user_input.split(",")]
print(pancake_sort(unsorted))
import doctest

doctest.testmod()