mirror of
https://github.com/TheAlgorithms/Python.git
synced 2026-09-28 13:33:11 +08:00
Fix merge sort comparable clean (#15259)
* updating DIRECTORY.md * sorts: support comparable items in merge sort * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * sorts: use type parameters in merge sort * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Fix grammar in merge sort docstring Corrected minor grammatical errors in docstring. * Update DIRECTORY.md with new algorithms and functions * updating DIRECTORY.md * updating DIRECTORY.md --------- Co-authored-by: kadubhumika <kadubhumika@users.noreply.github.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Christian Clauss <cclauss@me.com> Co-authored-by: cclauss <cclauss@users.noreply.github.com>
This commit is contained in:
co-authored by
kadubhumika
pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Christian Clauss
cclauss
parent
0372abc42b
commit
9e845982eb
@@ -1354,11 +1354,13 @@
|
||||
* [Ternary Search](searches/ternary_search.py)
|
||||
|
||||
## [Sorts](sorts)
|
||||
* [Adaptive Merge Sort](sorts/adaptive_merge_sort.py)
|
||||
* [Bead Sort](sorts/bead_sort.py)
|
||||
* [Binary Insertion Sort](sorts/binary_insertion_sort.py)
|
||||
* [Bitonic Sort](sorts/bitonic_sort.py)
|
||||
* [Bogo Sort](sorts/bogo_sort.py)
|
||||
* [Bubble Sort](sorts/bubble_sort.py)
|
||||
* [Bubble Sort Recursive](sorts/bubble_sort_recursive.py)
|
||||
* [Bucket Sort](sorts/bucket_sort.py)
|
||||
* [Circle Sort](sorts/circle_sort.py)
|
||||
* [Cocktail Shaker Sort](sorts/cocktail_shaker_sort.py)
|
||||
@@ -1376,6 +1378,7 @@
|
||||
* [Insertion Sort](sorts/insertion_sort.py)
|
||||
* [Intro Sort](sorts/intro_sort.py)
|
||||
* [Iterative Merge Sort](sorts/iterative_merge_sort.py)
|
||||
* [Kirkpatrick Reisch Sort](sorts/kirkpatrick_reisch_sort.py)
|
||||
* [Merge Insertion Sort](sorts/merge_insertion_sort.py)
|
||||
* [Merge Sort](sorts/merge_sort.py)
|
||||
* [Msd Radix Sort](sorts/msd_radix_sort.py)
|
||||
@@ -1387,16 +1390,20 @@
|
||||
* [Patience Sort](sorts/patience_sort.py)
|
||||
* [Pigeon Sort](sorts/pigeon_sort.py)
|
||||
* [Pigeonhole Sort](sorts/pigeonhole_sort.py)
|
||||
* [Power Sort](sorts/power_sort.py)
|
||||
* [Quick Sort](sorts/quick_sort.py)
|
||||
* [Quick Sort 3 Partition](sorts/quick_sort_3_partition.py)
|
||||
* [Radix Sort](sorts/radix_sort.py)
|
||||
* [Recursive Insertion Sort](sorts/recursive_insertion_sort.py)
|
||||
* [Recursive Mergesort Array](sorts/recursive_mergesort_array.py)
|
||||
* [Recursive Quick Sort](sorts/recursive_quick_sort.py)
|
||||
* [Reverse Selection](sorts/reverse_selection.py)
|
||||
* [Reversort](sorts/reversort.py)
|
||||
* [Selection Sort](sorts/selection_sort.py)
|
||||
* [Shell Sort](sorts/shell_sort.py)
|
||||
* [Shrink Shell Sort](sorts/shrink_shell_sort.py)
|
||||
* [Slowsort](sorts/slowsort.py)
|
||||
* [Smoothsort](sorts/smoothsort.py)
|
||||
* [Stalin Sort](sorts/stalin_sort.py)
|
||||
* [Stooge Sort](sorts/stooge_sort.py)
|
||||
* [Strand Sort](sorts/strand_sort.py)
|
||||
|
||||
+21
-10
@@ -1,21 +1,27 @@
|
||||
"""
|
||||
This is a pure Python implementation of the merge sort algorithm.
|
||||
The merge sort algorithm.
|
||||
|
||||
For doctests run following command:
|
||||
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:
|
||||
For manual testing, run:
|
||||
python merge_sort.py
|
||||
"""
|
||||
|
||||
from typing import Protocol
|
||||
|
||||
def merge_sort(collection: list) -> list:
|
||||
|
||||
class Comparable(Protocol):
|
||||
def __lt__(self, other: object, /) -> bool: ...
|
||||
|
||||
|
||||
def merge_sort[T: Comparable](collection: list[T]) -> list[T]:
|
||||
"""
|
||||
Sorts a list using the merge sort algorithm.
|
||||
|
||||
:param collection: A mutable ordered collection with comparable items.
|
||||
:return: The same collection ordered in ascending order.
|
||||
:param collection: A collection with comparable items.
|
||||
:return: The collection ordered in ascending order.
|
||||
|
||||
Time Complexity: O(n log n)
|
||||
Space Complexity: O(n)
|
||||
@@ -23,13 +29,15 @@ def merge_sort(collection: list) -> list:
|
||||
Examples:
|
||||
>>> merge_sort([0, 5, 3, 2, 2])
|
||||
[0, 2, 2, 3, 5]
|
||||
|
||||
>>> merge_sort([])
|
||||
[]
|
||||
>>> merge_sort([-2, -5, -45])
|
||||
|
||||
>>> merge_sort([-2, -45, -5])
|
||||
[-45, -5, -2]
|
||||
"""
|
||||
|
||||
def merge(left: list, right: list) -> list:
|
||||
def merge(left: list[T], right: list[T]) -> list[T]:
|
||||
"""
|
||||
Merge two sorted lists into a single sorted list.
|
||||
|
||||
@@ -37,9 +45,12 @@ def merge_sort(collection: list) -> list:
|
||||
:param right: Right collection
|
||||
:return: Merged result
|
||||
"""
|
||||
result = []
|
||||
result: list[T] = []
|
||||
while left and right:
|
||||
result.append(left.pop(0) if left[0] <= right[0] else right.pop(0))
|
||||
if right[0] < left[0]:
|
||||
result.append(right.pop(0))
|
||||
else:
|
||||
result.append(left.pop(0))
|
||||
result.extend(left)
|
||||
result.extend(right)
|
||||
return result
|
||||
|
||||
Reference in New Issue
Block a user