We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
2 parents 5bed476 + 839a841 commit ce77326Copy full SHA for ce77326
sorts/quick_sort.py
@@ -35,19 +35,26 @@ def quick_sort(collection):
35
>>> quick_sort([-2, -5, -45])
36
[-45, -5, -2]
37
"""
38
- if len(collection) <= 1:
+ total_elements = len(collection)
39
+
40
+ if total_elements <= 1:
41
return collection
42
less = []
43
equal = []
44
greater = []
45
pivot = collection[0]
- for x in collection:
- if x < pivot:
46
- less.append(x)
47
- elif x == pivot:
48
- equal.append(x)
+ equal.append(pivot)
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)
56
else:
- greater.append(x)
57
+ greater.append(element)
58
return quick_sort(less) + equal + quick_sort(greater)
59
60
0 commit comments