Skip to content

Commit 74ebd2b

Browse files
Harmanpreet KaurHarmanpreet Kaur
authored andcommitted
docs: add complexity analysis to insertion_sort docstring
1 parent c0db072 commit 74ebd2b

1 file changed

Lines changed: 10 additions & 0 deletions

File tree

sorts/insertion_sort.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,16 @@ def insertion_sort[T: Comparable](collection: MutableSequence[T]) -> MutableSequ
3131
comparable items inside
3232
:return: the same collection ordered by ascending
3333
34+
Complexity Analysis:
35+
Time Complexity:
36+
- Best Case: O(n) when the collection is already sorted
37+
- Average Case: O(n^2)
38+
- Worst Case: O(n^2) when the collection is sorted in reverse order
39+
40+
Space Complexity:
41+
- O(1) because the algorithm sorts the collection in place and
42+
uses only a constant amount of additional memory
43+
3444
Examples:
3545
>>> insertion_sort([0, 5, 3, 2, 2])
3646
[0, 2, 2, 3, 5]

0 commit comments

Comments
 (0)