Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions sorts/quick_sort.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,16 @@ def quick_sort(collection):
return collection
else:
pivot = collection[0]
greater = [element for element in collection[1:] if element > pivot]
lesser = [element for element in collection[1:] if element <= pivot]
# Modify the list comprehensions to reduce the number of judgments, the speed has increased by more than 50%.
greater = []
lesser = []
for element in collection[1:]:
if element > pivot:
greater.append(element)
else:
lesser.append(element)
# greater = [element for element in collection[1:] if element > pivot]
# lesser = [element for element in collection[1:] if element <= pivot]
return quick_sort(lesser) + [pivot] + quick_sort(greater)


Expand Down