Skip to content

Commit f6e5c75

Browse files
authored
Update quick_sort.py
1 parent f988f2c commit f6e5c75

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

sorts/quick_sort.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,14 @@ def _partition(collection: list, low: int, high: int) -> int:
5656
:param high: ending index of the partition
5757
:return: the final pivot index after partitioning
5858
"""
59-
# Randomly select a pivot index and swap with the last element
59+
# Randomly select a pivot index and swap with the last element
6060
pivot_index = randrange(low, high + 1)
61-
collection[pivot_index], collection[high] = collection[high], collection[pivot_index]
61+
62+
# Use a temporary variable for the swap to keep lines short
63+
temp_pivot = collection[pivot_index]
64+
collection[pivot_index] = collection[high]
65+
collection[high] = temp_pivot
66+
6267
pivot = collection[high]
6368

6469
# Index of smaller element (elements <= pivot will be moved to left of this)

0 commit comments

Comments
 (0)