From 74ebd2b09dcfbcaa1fa7a130dc532f6bb9a50f00 Mon Sep 17 00:00:00 2001 From: Harmanpreet Kaur Date: Wed, 15 Jul 2026 22:20:25 -0500 Subject: [PATCH] docs: add complexity analysis to insertion_sort docstring --- sorts/insertion_sort.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/sorts/insertion_sort.py b/sorts/insertion_sort.py index 2e39be255df7..24f53b654439 100644 --- a/sorts/insertion_sort.py +++ b/sorts/insertion_sort.py @@ -31,6 +31,16 @@ def insertion_sort[T: Comparable](collection: MutableSequence[T]) -> MutableSequ comparable items inside :return: the same collection ordered by ascending + Complexity Analysis: + Time Complexity: + - Best Case: O(n) when the collection is already sorted + - Average Case: O(n^2) + - Worst Case: O(n^2) when the collection is sorted in reverse order + + Space Complexity: + - O(1) because the algorithm sorts the collection in place and + uses only a constant amount of additional memory + Examples: >>> insertion_sort([0, 5, 3, 2, 2]) [0, 2, 2, 3, 5]