Skip to content

Commit a0ab3ce

Browse files
BruceLee569poyea
authored andcommitted
Update quick_sort.py (TheAlgorithms#830)
Modify the list comprehensions to reduce the number of judgments, the speed has increased by more than 50%.
1 parent 023f5e0 commit a0ab3ce

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

sorts/quick_sort.py

+10-2
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,16 @@ def quick_sort(collection):
3434
return collection
3535
else:
3636
pivot = collection[0]
37-
greater = [element for element in collection[1:] if element > pivot]
38-
lesser = [element for element in collection[1:] if element <= pivot]
37+
# Modify the list comprehensions to reduce the number of judgments, the speed has increased by more than 50%.
38+
greater = []
39+
lesser = []
40+
for element in collection[1:]:
41+
if element > pivot:
42+
greater.append(element)
43+
else:
44+
lesser.append(element)
45+
# greater = [element for element in collection[1:] if element > pivot]
46+
# lesser = [element for element in collection[1:] if element <= pivot]
3947
return quick_sort(lesser) + [pivot] + quick_sort(greater)
4048

4149

0 commit comments

Comments
 (0)