Skip to content

Commit ced91cd

Browse files
committed
sorts: make quick_sort_3_partition generic over Comparable items
1 parent d182474 commit ced91cd

2 files changed

Lines changed: 30 additions & 4 deletions

File tree

‎sorts/quick_sort_3_partition.py‎

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
1-
def quick_sort_3partition(sorting: list, left: int, right: int) -> None:
1+
from typing import Any, Protocol
2+
3+
4+
class Comparable(Protocol):
5+
def __lt__(self, other: Any, /) -> bool: ...
6+
7+
8+
def quick_sort_3partition[T: Comparable](
9+
sorting: list[T], left: int, right: int
10+
) -> None:
211
""" "
312
Python implementation of quick sort algorithm with 3-way partition.
413
The idea of 3-way quick sort is based on "Dutch National Flag algorithm".
@@ -21,6 +30,10 @@ def quick_sort_3partition(sorting: list, left: int, right: int) -> None:
2130
>>> quick_sort_3partition(array3, 0, 0)
2231
>>> array3
2332
[]
33+
>>> array4 = ["pear", "apple", "fig"]
34+
>>> quick_sort_3partition(array4, 0, 2)
35+
>>> array4
36+
['apple', 'fig', 'pear']
2437
"""
2538
if right <= left:
2639
return
@@ -41,7 +54,9 @@ def quick_sort_3partition(sorting: list, left: int, right: int) -> None:
4154
quick_sort_3partition(sorting, b + 1, right)
4255

4356

44-
def quick_sort_lomuto_partition(sorting: list, left: int, right: int) -> None:
57+
def quick_sort_lomuto_partition[T: Comparable](
58+
sorting: list[T], left: int, right: int
59+
) -> None:
4560
"""
4661
A pure Python implementation of quick sort algorithm(in-place)
4762
with Lomuto partition scheme:
@@ -65,18 +80,24 @@ def quick_sort_lomuto_partition(sorting: list, left: int, right: int) -> None:
6580
>>> quick_sort_lomuto_partition(nums3, 0, 3)
6681
>>> nums3
6782
[-4, -2, 0, 5]
83+
>>> nums4 = ["pear", "apple", "fig"]
84+
>>> quick_sort_lomuto_partition(nums4, 0, 2)
85+
>>> nums4
86+
['apple', 'fig', 'pear']
6887
"""
6988
if left < right:
7089
pivot_index = lomuto_partition(sorting, left, right)
7190
quick_sort_lomuto_partition(sorting, left, pivot_index - 1)
7291
quick_sort_lomuto_partition(sorting, pivot_index + 1, right)
7392

7493

75-
def lomuto_partition(sorting: list, left: int, right: int) -> int:
94+
def lomuto_partition[T: Comparable](sorting: list[T], left: int, right: int) -> int:
7695
"""
7796
Example:
7897
>>> lomuto_partition([1,5,7,6], 0, 3)
7998
2
99+
>>> lomuto_partition(["b", "a"], 0, 1)
100+
0
80101
"""
81102
pivot = sorting[right]
82103
store_index = left
@@ -88,7 +109,7 @@ def lomuto_partition(sorting: list, left: int, right: int) -> int:
88109
return store_index
89110

90111

91-
def three_way_radix_quicksort(sorting: list) -> list:
112+
def three_way_radix_quicksort[T: Comparable](sorting: list[T]) -> list[T]:
92113
"""
93114
Three-way radix quicksort:
94115
https://en.wikipedia.org/wiki/Quicksort#Three-way_radix_quicksort
@@ -103,6 +124,8 @@ def three_way_radix_quicksort(sorting: list) -> list:
103124
[-5, -2, -2, 0, 1, 1]
104125
>>> three_way_radix_quicksort([1, 2, 5, 1, 2, 0, 0, 5, 2, -1])
105126
[-1, 0, 0, 1, 1, 2, 2, 2, 5, 5]
127+
>>> three_way_radix_quicksort(["pear", "apple", "fig"])
128+
['apple', 'fig', 'pear']
106129
"""
107130
if len(sorting) <= 1:
108131
return sorting

‎tests/test_sorts.py‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
from sorts.pancake_sort import pancake_sort
4242
from sorts.patience_sort import patience_sort
4343
from sorts.quick_sort import quick_sort
44+
from sorts.quick_sort_3_partition import three_way_radix_quicksort
4445
from sorts.recursive_insertion_sort import rec_insertion_sort
4546
from sorts.recursive_mergesort_array import merge
4647
from sorts.reversort import reversort
@@ -85,6 +86,7 @@ def test_heap_sort() -> None:
8586
shrink_shell_sort,
8687
stooge_sort,
8788
strand_sort,
89+
three_way_radix_quicksort,
8890
)
8991

9092

@@ -153,6 +155,7 @@ def test_rec_insertion_sort(case) -> None:
153155
selection_sort,
154156
shrink_shell_sort,
155157
strand_sort,
158+
three_way_radix_quicksort,
156159
],
157160
ids=lambda f: f.__name__,
158161
)

0 commit comments

Comments
 (0)