Update quick_sort.py (#830)

Modify the list comprehensions to reduce the number of judgments, the speed has increased by more than 50%.
This commit is contained in:
BruceLee569 2019-05-24 23:54:03 +08:00 committed by John Law
parent 023f5e092d
commit a0ab3ce098

View File

@ -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)