Skip to content

Commit 66433a5

Browse files
authored
Update quick_sort.py
Streamlining quick_sort function
1 parent abd9d78 commit 66433a5

File tree

1 file changed

+11
-12
lines changed

1 file changed

+11
-12
lines changed

sorts/quick_sort.py

+11-12
Original file line numberDiff line numberDiff line change
@@ -35,21 +35,20 @@ def quick_sort(collection):
3535
>>> quick_sort([-2, -5, -45])
3636
[-45, -5, -2]
3737
"""
38+
if len(collection) <= 1:
39+
return collection
3840
less = []
3941
equal = []
4042
greater = []
41-
if len(collection) > 1:
42-
pivot = collection[0]
43-
for x in collection:
44-
if x < pivot:
45-
less.append(x)
46-
if x == pivot:
47-
equal.append(x)
48-
if x > pivot:
49-
greater.append(x)
50-
return quick_sort(less) + equal + quick_sort(greater)
51-
else:
52-
return collection
43+
pivot = collection[0]
44+
for x in collection:
45+
if x < pivot:
46+
less.append(x)
47+
elif x == pivot:
48+
equal.append(x)
49+
else:
50+
greater.append(x)
51+
return quick_sort(less) + equal + quick_sort(greater)
5352

5453

5554
if __name__ == '__main__':

0 commit comments

Comments
 (0)