diff --git a/sorts/tim_sort.py b/sorts/tim_sort.py index 2eeed88b7399..4a3c18b45735 100644 --- a/sorts/tim_sort.py +++ b/sorts/tim_sort.py @@ -2,6 +2,10 @@ def binary_search(lst: list[Any], item: Any, start: int, end: int) -> int: + """ + Find the index where an item should be inserted in a sorted list + """ + if start == end: return start if lst[start] > item else start + 1 if start > end: @@ -17,6 +21,9 @@ def binary_search(lst: list[Any], item: Any, start: int, end: int) -> int: def insertion_sort(lst: list[Any]) -> list[Any]: + """ + Sort a list using insertion sort and binary search + """ length = len(lst) for index in range(1, length): @@ -28,6 +35,9 @@ def insertion_sort(lst: list[Any]) -> list[Any]: def merge(left: list[Any], right: list[Any]) -> list[Any]: + """ + Merge two sorted lists into one sorted list + """ if not left: return right