diff --git a/sorts/insertion_sort.py b/sorts/insertion_sort.py index 2e39be255df7..5e85abb4da2e 100644 --- a/sorts/insertion_sort.py +++ b/sorts/insertion_sort.py @@ -13,6 +13,8 @@ python3 insertion_sort.py """ +from __future__ import annotations + from collections.abc import MutableSequence from typing import Any, Protocol, TypeVar @@ -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)): @@ -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) = }") diff --git a/sorts/pancake_sort.py b/sorts/pancake_sort.py index e5d600738435..606c161fef69 100644 --- a/sorts/pancake_sort.py +++ b/sorts/pancake_sort.py @@ -8,11 +8,15 @@ 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] @@ -20,6 +24,12 @@ 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: @@ -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()