Skip to content

Commit a30a283

Browse files
committed
Update sorts/quick_sort_3partition.py
Another quick sort algorithm, returns a new sorted list
1 parent 9016fe1 commit a30a283

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

Diff for: sorts/quick_sort_3_partition.py

+27-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
def quick_sort_3partition(sorting, left, right):
1+
def quick_sort_3partition(sorting: list, left: int, right: int) -> list:
22
if right <= left:
33
return
44
a = i = left
@@ -18,7 +18,33 @@ def quick_sort_3partition(sorting, left, right):
1818
quick_sort_3partition(sorting, b + 1, right)
1919

2020

21+
def quick_sort_3partition(sorting: list) -> list:
22+
"""
23+
Another quick sort algorithm, returns a new sorted list
24+
25+
>>> quick_sort_3partition([])
26+
[]
27+
>>> quick_sort_3partition([1])
28+
[1]
29+
>>> quick_sort_3partition([-5, -2, 1, -2, 0, 1])
30+
[-5, -2, -2, 0, 1, 1]
31+
>>> quick_sort_3partition([1, 2, 5, 1, 2, 0, 0, 5, 2, -1])
32+
[-1, 0, 0, 1, 1, 2, 2, 2, 5, 5]
33+
"""
34+
if len(sorting) <= 1:
35+
return sorting
36+
return (
37+
quick_sort_3partition([i for i in sorting if i < sorting[0]])
38+
+ [i for i in sorting if i == sorting[0]]
39+
+ quick_sort_3partition([i for i in sorting if i > sorting[0]])
40+
)
41+
42+
2143
if __name__ == "__main__":
44+
import doctest
45+
46+
doctest.testmod(verbose=True)
47+
2248
user_input = input("Enter numbers separated by a comma:\n").strip()
2349
unsorted = [int(item) for item in user_input.split(",")]
2450
quick_sort_3partition(unsorted, 0, len(unsorted) - 1)

0 commit comments

Comments
 (0)