diff --git a/sorts/bogo_sort.py b/sorts/bogo_sort.py index 9c133f0d8a55..d4b88ac26508 100644 --- a/sorts/bogo_sort.py +++ b/sorts/bogo_sort.py @@ -16,7 +16,7 @@ import random -def bogo_sort(collection): +def bogo_sort(collection: list[int]) -> list[int]: """Pure implementation of the bogosort algorithm in Python :param collection: some mutable ordered collection with heterogeneous comparable items inside @@ -30,7 +30,7 @@ def bogo_sort(collection): [-45, -5, -2] """ - def is_sorted(collection): + def is_sorted(collection: list[int]) -> bool: for i in range(len(collection) - 1): if collection[i] > collection[i + 1]: return False diff --git a/sorts/circle_sort.py b/sorts/circle_sort.py index 271fa1e8d58a..981ea2b83179 100644 --- a/sorts/circle_sort.py +++ b/sorts/circle_sort.py @@ -9,7 +9,7 @@ """ -def circle_sort(collection: list) -> list: +def circle_sort(collection: list[int]) -> list[int]: """A pure Python implementation of circle sort algorithm :param collection: a mutable collection of comparable items in any order @@ -30,7 +30,7 @@ def circle_sort(collection: list) -> list: if len(collection) < 2: return collection - def circle_sort_util(collection: list, low: int, high: int) -> bool: + def circle_sort_util(collection: list[int], low: int, high: int) -> bool: """ >>> arr = [5,4,3,2,1] >>> circle_sort_util(lst, 0, 2) diff --git a/sorts/cycle_sort.py b/sorts/cycle_sort.py index 7177c8ea110d..ac636860ed09 100644 --- a/sorts/cycle_sort.py +++ b/sorts/cycle_sort.py @@ -3,8 +3,12 @@ Source: https://en.wikipedia.org/wiki/Cycle_sort """ +from typing import TypeVar -def cycle_sort(array: list) -> list: +T = TypeVar("T", int, float) + + +def cycle_sort(array: list[T]) -> list[T]: """ >>> cycle_sort([4, 3, 2, 1]) [1, 2, 3, 4] @@ -51,3 +55,5 @@ def cycle_sort(array: list) -> list: if __name__ == "__main__": assert cycle_sort([4, 5, 3, 2, 1]) == [1, 2, 3, 4, 5] assert cycle_sort([0, 1, -10, 15, 2, -2]) == [-10, -2, 0, 1, 2, 15] + assert cycle_sort([-0.1, -0.2, 1.3, -0.8]) == [-0.8, -0.2, -0.1, 1.3] + print("All tests passed") diff --git a/sorts/merge_sort.py b/sorts/merge_sort.py index 11c202788035..10db332fa099 100644 --- a/sorts/merge_sort.py +++ b/sorts/merge_sort.py @@ -10,7 +10,7 @@ """ -def merge_sort(collection: list) -> list: +def merge_sort(collection: list[int]) -> list[int]: """ Sorts a list using the merge sort algorithm. @@ -29,7 +29,7 @@ def merge_sort(collection: list) -> list: [-45, -5, -2] """ - def merge(left: list, right: list) -> list: + def merge(left: list[int], right: list[int]) -> list[int]: """ Merge two sorted lists into a single sorted list. diff --git a/sorts/odd_even_sort.py b/sorts/odd_even_sort.py index 7dfe03054bc3..1543f52c23e4 100644 --- a/sorts/odd_even_sort.py +++ b/sorts/odd_even_sort.py @@ -5,7 +5,7 @@ """ -def odd_even_sort(input_list: list) -> list: +def odd_even_sort(input_list: list[int]) -> list[int]: """ Sort input with odd even sort.