mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-01-19 00:37:02 +00:00
Update quick_sort.py
Streamlining quick_sort function
This commit is contained in:
parent
abd9d78a62
commit
66433a5f0b
|
@ -35,21 +35,20 @@ def quick_sort(collection):
|
||||||
>>> quick_sort([-2, -5, -45])
|
>>> quick_sort([-2, -5, -45])
|
||||||
[-45, -5, -2]
|
[-45, -5, -2]
|
||||||
"""
|
"""
|
||||||
|
if len(collection) <= 1:
|
||||||
|
return collection
|
||||||
less = []
|
less = []
|
||||||
equal = []
|
equal = []
|
||||||
greater = []
|
greater = []
|
||||||
if len(collection) > 1:
|
|
||||||
pivot = collection[0]
|
pivot = collection[0]
|
||||||
for x in collection:
|
for x in collection:
|
||||||
if x < pivot:
|
if x < pivot:
|
||||||
less.append(x)
|
less.append(x)
|
||||||
if x == pivot:
|
elif x == pivot:
|
||||||
equal.append(x)
|
equal.append(x)
|
||||||
if x > pivot:
|
else:
|
||||||
greater.append(x)
|
greater.append(x)
|
||||||
return quick_sort(less) + equal + quick_sort(greater)
|
return quick_sort(less) + equal + quick_sort(greater)
|
||||||
else:
|
|
||||||
return collection
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|
Loading…
Reference in New Issue
Block a user