From 490ab4ecc8c70d3cef9c7b5b17035976dac310e2 Mon Sep 17 00:00:00 2001 From: Tushar Pamnani <121151091+tusharpamnani@users.noreply.github.com> Date: Tue, 5 Dec 2023 01:05:00 +0530 Subject: [PATCH 1/4] optimize quicksort implementation --- sorts/quick_sort.py | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/sorts/quick_sort.py b/sorts/quick_sort.py index b79d3eac3e48..1ac69eea7a10 100644 --- a/sorts/quick_sort.py +++ b/sorts/quick_sort.py @@ -8,15 +8,13 @@ python3 quick_sort.py """ from __future__ import annotations - from random import randrange - def quick_sort(collection: list) -> list: - """A pure Python implementation of quick sort algorithm + """A pure Python implementation of quicksort algorithm. :param collection: a mutable collection of comparable items - :return: the same collection ordered by ascending + :return: the same collection ordered in ascending order Examples: >>> quick_sort([0, 5, 3, 2, 2]) @@ -26,23 +24,25 @@ def quick_sort(collection: list) -> list: >>> quick_sort([-2, 5, 0, -45]) [-45, -2, 0, 5] """ + # Base case: if the collection has 0 or 1 elements, it is already sorted if len(collection) < 2: return collection - pivot_index = randrange(len(collection)) # Use random element as pivot - pivot = collection[pivot_index] - greater: list[int] = [] # All elements greater than pivot - lesser: list[int] = [] # All elements less than or equal to pivot - for element in collection[:pivot_index]: - (greater if element > pivot else lesser).append(element) + # Randomly select a pivot index and remove the pivot element from the collection + pivot_index = randrange(len(collection)) + pivot = collection.pop(pivot_index) - for element in collection[pivot_index + 1 :]: - (greater if element > pivot else lesser).append(element) + # Partition the remaining elements into two groups: lesser or equal, and greater + lesser = [item for item in collection if item <= pivot] + greater = [item for item in collection if item > pivot] + # Recursively apply quick_sort to the lesser and greater groups, and combine with the pivot return [*quick_sort(lesser), pivot, *quick_sort(greater)] - if __name__ == "__main__": + # Get user input and convert it into a list of integers user_input = input("Enter numbers separated by a comma:\n").strip() unsorted = [int(item) for item in user_input.split(",")] + + # Print the result of sorting the user-provided list print(quick_sort(unsorted)) From dae464de1daef15bdafaac4fd9dbda309abb2373 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 4 Dec 2023 19:41:30 +0000 Subject: [PATCH 2/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- sorts/quick_sort.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/sorts/quick_sort.py b/sorts/quick_sort.py index 1ac69eea7a10..d69b1f8a2a43 100644 --- a/sorts/quick_sort.py +++ b/sorts/quick_sort.py @@ -10,6 +10,7 @@ from __future__ import annotations from random import randrange + def quick_sort(collection: list) -> list: """A pure Python implementation of quicksort algorithm. @@ -39,6 +40,7 @@ def quick_sort(collection: list) -> list: # Recursively apply quick_sort to the lesser and greater groups, and combine with the pivot return [*quick_sort(lesser), pivot, *quick_sort(greater)] + if __name__ == "__main__": # Get user input and convert it into a list of integers user_input = input("Enter numbers separated by a comma:\n").strip() From cc4ac431302355b80b70e9eaa156f714af101c14 Mon Sep 17 00:00:00 2001 From: Tianyi Zheng Date: Tue, 19 Dec 2023 20:55:57 -0500 Subject: [PATCH 3/4] Apply suggestions from code review --- sorts/quick_sort.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sorts/quick_sort.py b/sorts/quick_sort.py index d69b1f8a2a43..32ba8ce4f40e 100644 --- a/sorts/quick_sort.py +++ b/sorts/quick_sort.py @@ -37,7 +37,7 @@ def quick_sort(collection: list) -> list: lesser = [item for item in collection if item <= pivot] greater = [item for item in collection if item > pivot] - # Recursively apply quick_sort to the lesser and greater groups, and combine with the pivot + # Recursively sort the lesser and greater groups, and combine with the pivot return [*quick_sort(lesser), pivot, *quick_sort(greater)] From 4cca34d23f313667274a590f8818bcfedb3b7fc7 Mon Sep 17 00:00:00 2001 From: Tianyi Zheng Date: Tue, 19 Dec 2023 20:57:06 -0500 Subject: [PATCH 4/4] Update quick_sort.py --- sorts/quick_sort.py | 1 + 1 file changed, 1 insertion(+) diff --git a/sorts/quick_sort.py b/sorts/quick_sort.py index 32ba8ce4f40e..6b95fc144426 100644 --- a/sorts/quick_sort.py +++ b/sorts/quick_sort.py @@ -8,6 +8,7 @@ python3 quick_sort.py """ from __future__ import annotations + from random import randrange