From 8af40514fc85563c0a207a8894cd962dff71c243 Mon Sep 17 00:00:00 2001 From: Shreya Date: Wed, 1 Jul 2026 18:23:40 +0530 Subject: [PATCH 1/2] Add docstrings to helper functions in tim_sort --- sorts/tim_sort.py | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/sorts/tim_sort.py b/sorts/tim_sort.py index 2eeed88b7399..08246e8cf50c 100644 --- a/sorts/tim_sort.py +++ b/sorts/tim_sort.py @@ -1,7 +1,30 @@ +""" +A pure Python implementation of the Tim Sort algorithm. + +For doctests run following command: +python -m doctest -v tim_sort.py +or +python3 -m doctest -v tim_sort.py + +For manual testing run: +python tim_sort.py +or +python3 tim_sort.py +""" + from typing import Any def binary_search(lst: list[Any], item: Any, start: int, end: int) -> int: + """ + Find the insertion position of an item in a sorted list. + + :param lst: A sorted list of comparable items. + :param item: The item to insert. + :param start: The starting index of the search range. + :param end: The ending index of the search range. + :return: The index where the item should be inserted. + """ if start == end: return start if lst[start] > item else start + 1 if start > end: @@ -17,6 +40,12 @@ 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 the insertion sort algorithm. + + :param lst: A list of comparable items. + :return: The sorted list. + """ length = len(lst) for index in range(1, length): @@ -28,6 +57,13 @@ def insertion_sort(lst: list[Any]) -> list[Any]: def merge(left: list[Any], right: list[Any]) -> list[Any]: + """ + Merge two sorted lists into a single sorted list. + + :param left: The left sorted list. + :param right: The right sorted list. + :return: A merged sorted list. + """ if not left: return right @@ -76,6 +112,9 @@ def tim_sort(lst: list[Any] | tuple[Any, ...] | str) -> list[Any]: def main(): + """ + Run a simple example of the Tim Sort algorithm. + """ lst = [5, 9, 10, 3, -4, 5, 178, 92, 46, -18, 0, 7] sorted_lst = tim_sort(lst) print(sorted_lst) From 92fdb7d6375d69578567c1cad7643597dac4deec Mon Sep 17 00:00:00 2001 From: Shreya Date: Mon, 6 Jul 2026 20:53:16 +0530 Subject: [PATCH 2/2] Add doctests to helper function docstrings --- sorts/tim_sort.py | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/sorts/tim_sort.py b/sorts/tim_sort.py index 08246e8cf50c..7f8b7b1e42c5 100644 --- a/sorts/tim_sort.py +++ b/sorts/tim_sort.py @@ -19,11 +19,10 @@ def binary_search(lst: list[Any], item: Any, start: int, end: int) -> int: """ Find the insertion position of an item in a sorted list. - :param lst: A sorted list of comparable items. - :param item: The item to insert. - :param start: The starting index of the search range. - :param end: The ending index of the search range. - :return: The index where the item should be inserted. + >>> binary_search([1, 3, 5, 7], 4, 0, 3) + 2 + >>> binary_search([1, 3, 5, 7], 8, 0, 3) + 4 """ if start == end: return start if lst[start] > item else start + 1 @@ -43,8 +42,12 @@ def insertion_sort(lst: list[Any]) -> list[Any]: """ Sort a list using the insertion sort algorithm. - :param lst: A list of comparable items. - :return: The sorted list. + >>> insertion_sort([3, 2, 1]) + [1, 2, 3] + >>> insertion_sort([1]) + [1] + >>> insertion_sort([]) + [] """ length = len(lst) @@ -60,9 +63,12 @@ def merge(left: list[Any], right: list[Any]) -> list[Any]: """ Merge two sorted lists into a single sorted list. - :param left: The left sorted list. - :param right: The right sorted list. - :return: A merged sorted list. + >>> merge([1, 3, 5], [2, 4, 6]) + [1, 2, 3, 4, 5, 6] + >>> merge([], [1, 2]) + [1, 2] + >>> merge([1, 2], []) + [1, 2] """ if not left: return right