Skip to content

Commit 9d1c587

Browse files
authored
Refactor quick_sort_3_partition.py for clarity
Refactor import statement and improve docstrings for clarity.
1 parent 974da8d commit 9d1c587

1 file changed

Lines changed: 6 additions & 7 deletions

File tree

‎sorts/quick_sort_3_partition.py‎

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import random
1+
from random import randrange
22

33

44
def quick_sort_3partition(sorting: list, left: int, right: int) -> None:
55
""" "
6-
Python implementation of quick sort algorithm with 3-way partition.
7-
The idea of 3-way quick sort is based on "Dutch National Flag algorithm".
6+
Python implementation of the quicksort algorithm with 3-way partition.
7+
The idea of 3-way quicksort is based on "Dutch National Flag algorithm".
88
99
:param sorting: sort list
1010
:param left: left endpoint of sorting
@@ -46,7 +46,7 @@ def quick_sort_3partition(sorting: list, left: int, right: int) -> None:
4646

4747
def quick_sort_lomuto_partition(sorting: list, left: int, right: int) -> None:
4848
"""
49-
A pure Python implementation of quick sort algorithm(in-place)
49+
A pure Python implementation of the quicksort algorithm(in-place)
5050
with Lomuto partition scheme:
5151
https://en.wikipedia.org/wiki/Quicksort#Lomuto_partition_scheme
5252
@@ -133,7 +133,7 @@ def hoare_partition_by_value(
133133
| |
134134
left right
135135
136-
Where the middle values are [u]nknown, since they are not yet traversed.
136+
Where the middle values are unknown (u), since they are not yet traversed.
137137
`left-1` points to the end of the left subarray.
138138
`right+1` points to the start of the right subarray.
139139
"""
@@ -218,8 +218,7 @@ def quicksort_hoare(array: list, start: int = 0, end: int | None = None):
218218
if end + 1 - start <= 1:
219219
return
220220

221-
pivot_index = random.randrange(start, end)
222-
pivot_index_final = hoare_partition_by_pivot(array, pivot_index, start, end)
221+
pivot_index_final = hoare_partition_by_pivot(array, randrange(start, end), start, end)
223222
quicksort_hoare(array, start, pivot_index_final - 1)
224223
quicksort_hoare(array, pivot_index_final + 1, end)
225224

0 commit comments

Comments
 (0)