Skip to content

Commit 2dfa10d

Browse files
Add time and space complexity analysis to insertion_sort docstring
Document the performance characteristics of insertion sort in the function docstring: - Time Complexity: O(n^2) worst/average case, O(n) best case (already sorted input) - Space Complexity: O(1), sorts in place Fixes #14866
1 parent f5988cc commit 2dfa10d

1 file changed

Lines changed: 6 additions & 0 deletions

File tree

sorts/insertion_sort.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ def insertion_sort[T: Comparable](collection: MutableSequence[T]) -> MutableSequ
3131
comparable items inside
3232
:return: the same collection ordered by ascending
3333
34+
Time Complexity:
35+
- O(n^2) in the worst and average cases
36+
- O(n) in the best case, when the collection is already sorted
37+
Space Complexity:
38+
- O(1), since the collection is sorted in place
39+
3440
Examples:
3541
>>> insertion_sort([0, 5, 3, 2, 2])
3642
[0, 2, 2, 3, 5]

0 commit comments

Comments
 (0)