mirror of
https://hub.njuu.cf/TheAlgorithms/Python.git
synced 2023-10-11 13:06:12 +08:00
Tighten up quicksort() (#3319)
* Tighten up quicksort() * updating DIRECTORY.md * str does not support .pop() Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
This commit is contained in:
parent
f0033f87e0
commit
2e90debab3
@ -738,6 +738,7 @@
|
|||||||
* [Iterative Merge Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/iterative_merge_sort.py)
|
* [Iterative Merge Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/iterative_merge_sort.py)
|
||||||
* [Merge Insertion Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/merge_insertion_sort.py)
|
* [Merge Insertion Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/merge_insertion_sort.py)
|
||||||
* [Merge Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/merge_sort.py)
|
* [Merge Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/merge_sort.py)
|
||||||
|
* [Natural Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/natural_sort.py)
|
||||||
* [Odd Even Transposition Parallel](https://github.com/TheAlgorithms/Python/blob/master/sorts/odd_even_transposition_parallel.py)
|
* [Odd Even Transposition Parallel](https://github.com/TheAlgorithms/Python/blob/master/sorts/odd_even_transposition_parallel.py)
|
||||||
* [Odd Even Transposition Single Threaded](https://github.com/TheAlgorithms/Python/blob/master/sorts/odd_even_transposition_single_threaded.py)
|
* [Odd Even Transposition Single Threaded](https://github.com/TheAlgorithms/Python/blob/master/sorts/odd_even_transposition_single_threaded.py)
|
||||||
* [Pancake Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/pancake_sort.py)
|
* [Pancake Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/pancake_sort.py)
|
||||||
|
@ -1,48 +1,36 @@
|
|||||||
"""
|
"""
|
||||||
This is a pure Python implementation of the quick sort algorithm
|
A pure Python implementation of the quick sort algorithm
|
||||||
|
|
||||||
For doctests run following command:
|
For doctests run following command:
|
||||||
python -m doctest -v quick_sort.py
|
|
||||||
or
|
|
||||||
python3 -m doctest -v quick_sort.py
|
python3 -m doctest -v quick_sort.py
|
||||||
|
|
||||||
For manual testing run:
|
For manual testing run:
|
||||||
python quick_sort.py
|
python3 quick_sort.py
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
def quick_sort(collection):
|
def quick_sort(collection: list) -> list:
|
||||||
"""Pure implementation of quick sort algorithm in Python
|
"""A pure Python implementation of quick sort algorithm
|
||||||
|
|
||||||
:param collection: some mutable ordered collection with heterogeneous
|
:param collection: a mutable collection of comparable items
|
||||||
comparable items inside
|
|
||||||
:return: the same collection ordered by ascending
|
:return: the same collection ordered by ascending
|
||||||
|
|
||||||
Examples:
|
Examples:
|
||||||
>>> quick_sort([0, 5, 3, 2, 2])
|
>>> quick_sort([0, 5, 3, 2, 2])
|
||||||
[0, 2, 2, 3, 5]
|
[0, 2, 2, 3, 5]
|
||||||
|
|
||||||
>>> quick_sort([])
|
>>> quick_sort([])
|
||||||
[]
|
[]
|
||||||
|
>>> quick_sort([-2, 5, 0, -45])
|
||||||
>>> quick_sort([-2, -5, -45])
|
[-45, -2, 0, 5]
|
||||||
[-45, -5, -2]
|
|
||||||
"""
|
"""
|
||||||
length = len(collection)
|
if len(collection) < 2:
|
||||||
if length <= 1:
|
|
||||||
return collection
|
return collection
|
||||||
else:
|
pivot = collection.pop() # Use the last element as the first pivot
|
||||||
# Use the last element as the first pivot
|
greater = [] # All elements greater than pivot
|
||||||
pivot = collection.pop()
|
lesser = [] # All elements less than or equal to pivot
|
||||||
# Put elements greater than pivot in greater list
|
for element in collection:
|
||||||
# Put elements lesser than pivot in lesser list
|
(greater if element > pivot else lesser).append(element)
|
||||||
greater, lesser = [], []
|
return quick_sort(lesser) + [pivot] + quick_sort(greater)
|
||||||
for element in collection:
|
|
||||||
if element > pivot:
|
|
||||||
greater.append(element)
|
|
||||||
else:
|
|
||||||
lesser.append(element)
|
|
||||||
return quick_sort(lesser) + [pivot] + quick_sort(greater)
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
Loading…
Reference in New Issue
Block a user