Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions sorts/insertion_sort.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down