mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-01-19 00:37:02 +00:00
updated version
This commit is contained in:
parent
5bed476a0a
commit
839a84182c
|
@ -35,19 +35,26 @@ def quick_sort(collection):
|
||||||
>>> quick_sort([-2, -5, -45])
|
>>> quick_sort([-2, -5, -45])
|
||||||
[-45, -5, -2]
|
[-45, -5, -2]
|
||||||
"""
|
"""
|
||||||
if len(collection) <= 1:
|
total_elements = len(collection)
|
||||||
|
|
||||||
|
if total_elements <= 1:
|
||||||
return collection
|
return collection
|
||||||
less = []
|
less = []
|
||||||
equal = []
|
equal = []
|
||||||
greater = []
|
greater = []
|
||||||
pivot = collection[0]
|
pivot = collection[0]
|
||||||
for x in collection:
|
|
||||||
if x < pivot:
|
equal.append(pivot)
|
||||||
less.append(x)
|
|
||||||
elif x == pivot:
|
for i in range(1, total_elements):
|
||||||
equal.append(x)
|
element = collection[i]
|
||||||
|
|
||||||
|
if element < pivot:
|
||||||
|
less.append(element)
|
||||||
|
elif element == pivot:
|
||||||
|
equal.append(element)
|
||||||
else:
|
else:
|
||||||
greater.append(x)
|
greater.append(element)
|
||||||
return quick_sort(less) + equal + quick_sort(greater)
|
return quick_sort(less) + equal + quick_sort(greater)
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user