sorts.merge_sort¶
The merge sort algorithm.
For doctests, run the following command: python -m doctest -v merge_sort.py or python3 -m doctest -v merge_sort.py For manual testing, run: python merge_sort.py
Attributes¶
Classes¶
Base class for protocol classes. |
Functions¶
|
Sorts a list using the merge sort algorithm. |
Module Contents¶
- class sorts.merge_sort.Comparable¶
Bases:
ProtocolBase class for protocol classes.
Protocol classes are defined as:
class Proto(Protocol): def meth(self) -> int: ...
Such classes are primarily used with static type checkers that recognize structural subtyping (static duck-typing).
For example:
class C: def meth(self) -> int: return 0 def func(x: Proto) -> int: return x.meth() func(C()) # Passes static type check
See PEP 544 for details. Protocol classes decorated with @typing.runtime_checkable act as simple-minded runtime protocols that check only the presence of given attributes, ignoring their type signatures. Protocol classes can be generic, they are defined as:
class GenProto[T](Protocol): def meth(self) -> T: ...
- __lt__(other: object, /) bool¶
- sorts.merge_sort.merge_sort[T: Comparable](collection: list[T]) list[T]¶
Sorts a list using the merge sort algorithm.
- Parameters:
collection – A collection with comparable items.
- Returns:
The collection ordered in ascending order.
Time Complexity: O(n log n) Space Complexity: O(n)
Examples: >>> merge_sort([0, 5, 3, 2, 2]) [0, 2, 2, 3, 5]
>>> merge_sort([]) []
>>> merge_sort([-2, -45, -5]) [-45, -5, -2]
- sorts.merge_sort.user_input¶