Skip to content

Commit 9e84598

Browse files
kadubhumikapre-commit-ci[bot]cclauss
authored
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>
1 parent 0372abc commit 9e84598

2 files changed

Lines changed: 28 additions & 10 deletions

File tree

DIRECTORY.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1354,11 +1354,13 @@
13541354
* [Ternary Search](searches/ternary_search.py)
13551355

13561356
## [Sorts](sorts)
1357+
* [Adaptive Merge Sort](sorts/adaptive_merge_sort.py)
13571358
* [Bead Sort](sorts/bead_sort.py)
13581359
* [Binary Insertion Sort](sorts/binary_insertion_sort.py)
13591360
* [Bitonic Sort](sorts/bitonic_sort.py)
13601361
* [Bogo Sort](sorts/bogo_sort.py)
13611362
* [Bubble Sort](sorts/bubble_sort.py)
1363+
* [Bubble Sort Recursive](sorts/bubble_sort_recursive.py)
13621364
* [Bucket Sort](sorts/bucket_sort.py)
13631365
* [Circle Sort](sorts/circle_sort.py)
13641366
* [Cocktail Shaker Sort](sorts/cocktail_shaker_sort.py)
@@ -1376,6 +1378,7 @@
13761378
* [Insertion Sort](sorts/insertion_sort.py)
13771379
* [Intro Sort](sorts/intro_sort.py)
13781380
* [Iterative Merge Sort](sorts/iterative_merge_sort.py)
1381+
* [Kirkpatrick Reisch Sort](sorts/kirkpatrick_reisch_sort.py)
13791382
* [Merge Insertion Sort](sorts/merge_insertion_sort.py)
13801383
* [Merge Sort](sorts/merge_sort.py)
13811384
* [Msd Radix Sort](sorts/msd_radix_sort.py)
@@ -1387,16 +1390,20 @@
13871390
* [Patience Sort](sorts/patience_sort.py)
13881391
* [Pigeon Sort](sorts/pigeon_sort.py)
13891392
* [Pigeonhole Sort](sorts/pigeonhole_sort.py)
1393+
* [Power Sort](sorts/power_sort.py)
13901394
* [Quick Sort](sorts/quick_sort.py)
13911395
* [Quick Sort 3 Partition](sorts/quick_sort_3_partition.py)
13921396
* [Radix Sort](sorts/radix_sort.py)
13931397
* [Recursive Insertion Sort](sorts/recursive_insertion_sort.py)
13941398
* [Recursive Mergesort Array](sorts/recursive_mergesort_array.py)
13951399
* [Recursive Quick Sort](sorts/recursive_quick_sort.py)
1400+
* [Reverse Selection](sorts/reverse_selection.py)
1401+
* [Reversort](sorts/reversort.py)
13961402
* [Selection Sort](sorts/selection_sort.py)
13971403
* [Shell Sort](sorts/shell_sort.py)
13981404
* [Shrink Shell Sort](sorts/shrink_shell_sort.py)
13991405
* [Slowsort](sorts/slowsort.py)
1406+
* [Smoothsort](sorts/smoothsort.py)
14001407
* [Stalin Sort](sorts/stalin_sort.py)
14011408
* [Stooge Sort](sorts/stooge_sort.py)
14021409
* [Strand Sort](sorts/strand_sort.py)

sorts/merge_sort.py

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,56 @@
11
"""
2-
This is a pure Python implementation of the merge sort algorithm.
2+
The merge sort algorithm.
33
4-
For doctests run following command:
4+
For doctests, run the following command:
55
python -m doctest -v merge_sort.py
66
or
77
python3 -m doctest -v merge_sort.py
8-
For manual testing run:
8+
For manual testing, run:
99
python merge_sort.py
1010
"""
1111

12+
from typing import Protocol
1213

13-
def merge_sort(collection: list) -> list:
14+
15+
class Comparable(Protocol):
16+
def __lt__(self, other: object, /) -> bool: ...
17+
18+
19+
def merge_sort[T: Comparable](collection: list[T]) -> list[T]:
1420
"""
1521
Sorts a list using the merge sort algorithm.
1622
17-
:param collection: A mutable ordered collection with comparable items.
18-
:return: The same collection ordered in ascending order.
23+
:param collection: A collection with comparable items.
24+
:return: The collection ordered in ascending order.
1925
2026
Time Complexity: O(n log n)
2127
Space Complexity: O(n)
2228
2329
Examples:
2430
>>> merge_sort([0, 5, 3, 2, 2])
2531
[0, 2, 2, 3, 5]
32+
2633
>>> merge_sort([])
2734
[]
28-
>>> merge_sort([-2, -5, -45])
35+
36+
>>> merge_sort([-2, -45, -5])
2937
[-45, -5, -2]
3038
"""
3139

32-
def merge(left: list, right: list) -> list:
40+
def merge(left: list[T], right: list[T]) -> list[T]:
3341
"""
3442
Merge two sorted lists into a single sorted list.
3543
3644
:param left: Left collection
3745
:param right: Right collection
3846
:return: Merged result
3947
"""
40-
result = []
48+
result: list[T] = []
4149
while left and right:
42-
result.append(left.pop(0) if left[0] <= right[0] else right.pop(0))
50+
if right[0] < left[0]:
51+
result.append(right.pop(0))
52+
else:
53+
result.append(left.pop(0))
4354
result.extend(left)
4455
result.extend(right)
4556
return result

0 commit comments

Comments
 (0)