Skip to content

Commit f1cb2c9

Browse files
cclaussgithub-actions
authored andcommitted
Tighten up quicksort() (TheAlgorithms#3319)
* Tighten up quicksort() * updating DIRECTORY.md * str does not support .pop() Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
1 parent fc0178d commit f1cb2c9

File tree

2 files changed

+15
-26
lines changed

2 files changed

+15
-26
lines changed

Diff for: DIRECTORY.md

+1
Original file line numberDiff line numberDiff line change
@@ -738,6 +738,7 @@
738738
* [Iterative Merge Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/iterative_merge_sort.py)
739739
* [Merge Insertion Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/merge_insertion_sort.py)
740740
* [Merge Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/merge_sort.py)
741+
* [Natural Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/natural_sort.py)
741742
* [Odd Even Transposition Parallel](https://github.com/TheAlgorithms/Python/blob/master/sorts/odd_even_transposition_parallel.py)
742743
* [Odd Even Transposition Single Threaded](https://github.com/TheAlgorithms/Python/blob/master/sorts/odd_even_transposition_single_threaded.py)
743744
* [Pancake Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/pancake_sort.py)

Diff for: sorts/quick_sort.py

+14-26
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,36 @@
11
"""
2-
This is a pure Python implementation of the quick sort algorithm
2+
A pure Python implementation of the quick sort algorithm
33
44
For doctests run following command:
5-
python -m doctest -v quick_sort.py
6-
or
75
python3 -m doctest -v quick_sort.py
86
97
For manual testing run:
10-
python quick_sort.py
8+
python3 quick_sort.py
119
"""
1210

1311

14-
def quick_sort(collection):
15-
"""Pure implementation of quick sort algorithm in Python
12+
def quick_sort(collection: list) -> list:
13+
"""A pure Python implementation of quick sort algorithm
1614
17-
:param collection: some mutable ordered collection with heterogeneous
18-
comparable items inside
15+
:param collection: a mutable collection of comparable items
1916
:return: the same collection ordered by ascending
2017
2118
Examples:
2219
>>> quick_sort([0, 5, 3, 2, 2])
2320
[0, 2, 2, 3, 5]
24-
2521
>>> quick_sort([])
2622
[]
27-
28-
>>> quick_sort([-2, -5, -45])
29-
[-45, -5, -2]
23+
>>> quick_sort([-2, 5, 0, -45])
24+
[-45, -2, 0, 5]
3025
"""
31-
length = len(collection)
32-
if length <= 1:
26+
if len(collection) < 2:
3327
return collection
34-
else:
35-
# Use the last element as the first pivot
36-
pivot = collection.pop()
37-
# Put elements greater than pivot in greater list
38-
# Put elements lesser than pivot in lesser list
39-
greater, lesser = [], []
40-
for element in collection:
41-
if element > pivot:
42-
greater.append(element)
43-
else:
44-
lesser.append(element)
45-
return quick_sort(lesser) + [pivot] + quick_sort(greater)
28+
pivot = collection.pop() # Use the last element as the first pivot
29+
greater = [] # All elements greater than pivot
30+
lesser = [] # All elements less than or equal to pivot
31+
for element in collection:
32+
(greater if element > pivot else lesser).append(element)
33+
return quick_sort(lesser) + [pivot] + quick_sort(greater)
4634

4735

4836
if __name__ == "__main__":

0 commit comments

Comments
 (0)