From c6a2ddd4e54a720c4f533a0549b0e0d2934d848e Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Thu, 15 Oct 2020 14:14:49 +0200 Subject: [PATCH 1/3] Tighten up quicksort() --- sorts/quick_sort.py | 45 +++++++++++++++++++-------------------------- 1 file changed, 19 insertions(+), 26 deletions(-) diff --git a/sorts/quick_sort.py b/sorts/quick_sort.py index f2a55c58b437..cf2f329f8b0d 100644 --- a/sorts/quick_sort.py +++ b/sorts/quick_sort.py @@ -1,48 +1,41 @@ """ -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: -python -m doctest -v quick_sort.py -or python3 -m doctest -v quick_sort.py For manual testing run: -python quick_sort.py +python3 quick_sort.py """ -def quick_sort(collection): - """Pure implementation of quick sort algorithm in Python +def quick_sort(collection: list) -> list: + """A pure Python implementation of quick sort algorithm - :param collection: some mutable ordered collection with heterogeneous - comparable items inside + :param collection: a collection of comparable items :return: the same collection ordered by ascending Examples: >>> quick_sort([0, 5, 3, 2, 2]) [0, 2, 2, 3, 5] - >>> quick_sort([]) [] - - >>> quick_sort([-2, -5, -45]) - [-45, -5, -2] + >>> quick_sort([-2, 5, 0, -45]) + [-45, -2, 0, 5] + >>> quick_sort("Python") + ['P', 'h', 'n', 'o', 't', 'y'] """ - length = len(collection) - if length <= 1: + if len(collection) < 2: return collection - else: - # Use the last element as the first pivot - pivot = collection.pop() - # Put elements greater than pivot in greater list - # Put elements lesser than pivot in lesser list - greater, lesser = [], [] - for element in collection: - if element > pivot: - greater.append(element) - else: - lesser.append(element) - return quick_sort(lesser) + [pivot] + quick_sort(greater) + pivot = collection.pop() # Use the last element as the first pivot + greater = [] # All elements greater than pivot + lesser = [] # All elements less than or equal to pivot + 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__": From baeb980cceb7894e77438ea9f29c14b4934235c1 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Thu, 15 Oct 2020 12:15:20 +0000 Subject: [PATCH 2/3] updating DIRECTORY.md --- DIRECTORY.md | 1 + 1 file changed, 1 insertion(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index d2ce99e7160d..2cf51f8c4beb 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -738,6 +738,7 @@ * [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 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 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) From 9cb7224b549888c3d0525019d82d6434cb96a898 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Thu, 15 Oct 2020 14:23:23 +0200 Subject: [PATCH 3/3] str does not support .pop() --- sorts/quick_sort.py | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/sorts/quick_sort.py b/sorts/quick_sort.py index cf2f329f8b0d..c6687a7fa8d5 100644 --- a/sorts/quick_sort.py +++ b/sorts/quick_sort.py @@ -12,7 +12,7 @@ def quick_sort(collection: list) -> list: """A pure Python implementation of quick sort algorithm - :param collection: a collection of comparable items + :param collection: a mutable collection of comparable items :return: the same collection ordered by ascending Examples: @@ -22,8 +22,6 @@ def quick_sort(collection: list) -> list: [] >>> quick_sort([-2, 5, 0, -45]) [-45, -2, 0, 5] - >>> quick_sort("Python") - ['P', 'h', 'n', 'o', 't', 'y'] """ if len(collection) < 2: return collection @@ -31,10 +29,7 @@ def quick_sort(collection: list) -> list: greater = [] # All elements greater than pivot lesser = [] # All elements less than or equal to pivot for element in collection: - if element > pivot: - greater.append(element) - else: - lesser.append(element) + (greater if element > pivot else lesser).append(element) return quick_sort(lesser) + [pivot] + quick_sort(greater)