Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) = }")
15 changes: 11 additions & 4 deletions sorts/pancake_sort.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@
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
Expand All @@ -20,6 +24,9 @@ def pancake_sort(arr):
[]
>>> 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 +41,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()