Skip to content

Commit a4ca28a

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent c51ac22 commit a4ca28a

1 file changed

Lines changed: 23 additions & 23 deletions

File tree

sorts/quick_sort.py

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
def insertion_sort(collection: list, left: int, right: int) -> None:
1717
"""Insertion sort for small arrays (optimization for quicksort).
18-
18+
1919
Args:
2020
collection: List to sort
2121
left: Starting index
@@ -32,13 +32,13 @@ def insertion_sort(collection: list, left: int, right: int) -> None:
3232

3333
def median_of_three(collection: list, left: int, mid: int, right: int) -> int:
3434
"""Return the index of the median of three elements.
35-
35+
3636
Args:
3737
collection: List to find median in
3838
left: Left index
39-
mid: Middle index
39+
mid: Middle index
4040
right: Right index
41-
41+
4242
Returns:
4343
Index of the median element
4444
"""
@@ -53,74 +53,74 @@ def median_of_three(collection: list, left: int, mid: int, right: int) -> int:
5353

5454
def partition_hoare(collection: list, left: int, right: int) -> int:
5555
"""Hoare partition scheme for quicksort.
56-
56+
5757
Args:
5858
collection: List to partition
5959
left: Left boundary
6060
right: Right boundary
61-
61+
6262
Returns:
6363
Final position of pivot
6464
"""
6565
# Use median of three for better pivot selection
6666
mid = left + (right - left) // 2
6767
pivot_idx = median_of_three(collection, left, mid, right)
68-
68+
6969
# Move pivot to the beginning
7070
collection[left], collection[pivot_idx] = collection[pivot_idx], collection[left]
7171
pivot = collection[left]
72-
72+
7373
i, j = left - 1, right + 1
74-
74+
7575
while True:
7676
i += 1
7777
while collection[i] < pivot:
7878
i += 1
79-
79+
8080
j -= 1
8181
while collection[j] > pivot:
8282
j -= 1
83-
83+
8484
if i >= j:
8585
return j
86-
86+
8787
collection[i], collection[j] = collection[j], collection[i]
8888

8989

9090
def quick_sort_inplace(collection: list, left: int = 0, right: int = None) -> None:
9191
"""Optimized in-place quicksort with multiple optimizations.
92-
92+
9393
Optimizations:
9494
- In-place partitioning (O(1) extra space vs O(n))
9595
- Median-of-three pivot selection
9696
- Hybrid with insertion sort for small arrays
9797
- Hoare partition scheme (better for duplicates)
98-
98+
9999
Args:
100100
collection: List to sort (modified in-place)
101101
left: Left boundary
102102
right: Right boundary
103103
"""
104104
if right is None:
105105
right = len(collection) - 1
106-
106+
107107
if left < right:
108108
# Use insertion sort for small arrays (optimization)
109109
if right - left < 10:
110110
insertion_sort(collection, left, right)
111111
return
112-
112+
113113
# Partition and get pivot position
114114
pivot_pos = partition_hoare(collection, left, right)
115-
115+
116116
# Recursively sort left and right partitions
117117
quick_sort_inplace(collection, left, pivot_pos)
118118
quick_sort_inplace(collection, pivot_pos + 1, right)
119119

120120

121121
def quick_sort(collection: list) -> list:
122122
"""A pure Python implementation of quicksort algorithm.
123-
123+
124124
This is the original implementation kept for compatibility.
125125
For better performance, use quick_sort_optimized().
126126
@@ -153,19 +153,19 @@ def quick_sort(collection: list) -> list:
153153

154154
def quick_sort_optimized(collection: list) -> list:
155155
"""Optimized quicksort with in-place partitioning and multiple optimizations.
156-
156+
157157
Performance improvements:
158158
- O(1) extra space vs O(n) in original
159159
- Better pivot selection (median of three)
160160
- Hybrid with insertion sort for small arrays
161161
- More efficient partitioning
162-
162+
163163
Args:
164164
collection: List to sort
165-
165+
166166
Returns:
167167
Sorted list
168-
168+
169169
Examples:
170170
>>> quick_sort_optimized([0, 5, 3, 2, 2])
171171
[0, 2, 2, 3, 5]
@@ -178,7 +178,7 @@ def quick_sort_optimized(collection: list) -> list:
178178
"""
179179
if len(collection) < 2:
180180
return collection
181-
181+
182182
# Create a copy to avoid modifying the original
183183
result = collection.copy()
184184
quick_sort_inplace(result)

0 commit comments

Comments
 (0)