Skip to content

Commit ce77326

Browse files
authored
Merge pull request TheAlgorithms#59 from alvin562/master
updated version
2 parents 5bed476 + 839a841 commit ce77326

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

sorts/quick_sort.py

+14-7
Original file line numberDiff line numberDiff line change
@@ -35,19 +35,26 @@ def quick_sort(collection):
3535
>>> quick_sort([-2, -5, -45])
3636
[-45, -5, -2]
3737
"""
38-
if len(collection) <= 1:
38+
total_elements = len(collection)
39+
40+
if total_elements <= 1:
3941
return collection
4042
less = []
4143
equal = []
4244
greater = []
4345
pivot = collection[0]
44-
for x in collection:
45-
if x < pivot:
46-
less.append(x)
47-
elif x == pivot:
48-
equal.append(x)
46+
47+
equal.append(pivot)
48+
49+
for i in range(1, total_elements):
50+
element = collection[i]
51+
52+
if element < pivot:
53+
less.append(element)
54+
elif element == pivot:
55+
equal.append(element)
4956
else:
50-
greater.append(x)
57+
greater.append(element)
5158
return quick_sort(less) + equal + quick_sort(greater)
5259

5360

0 commit comments

Comments
 (0)