From b9d2a3400a474c522f2286b4efde10b2759abdf0 Mon Sep 17 00:00:00 2001 From: Tushar Pamnani <121151091+tusharpamnani@users.noreply.github.com> Date: Tue, 5 Dec 2023 01:18:24 +0530 Subject: [PATCH 1/2] optimize quicksort implementation --- sorts/{quick_sort.py => quicksort.py} | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) rename sorts/{quick_sort.py => quicksort.py} (52%) diff --git a/sorts/quick_sort.py b/sorts/quicksort.py similarity index 52% rename from sorts/quick_sort.py rename to sorts/quicksort.py index b79d3eac3e48..1b18fc7135db 100644 --- a/sorts/quick_sort.py +++ b/sorts/quicksort.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 choose a pivot element and remove it 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 lists: lesser and greater + lesser = [item for item in collection if item <= pivot] + greater = [item for item in collection if item > pivot] + # Recursively sort the lesser and greater lists and concatenate them with the pivot return [*quick_sort(lesser), pivot, *quick_sort(greater)] - if __name__ == "__main__": + # Get user input as 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 input list using quicksort print(quick_sort(unsorted)) From 74ccb5b5cc93b3da2b8697d2708aea9baa7444ce 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:50:00 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- sorts/quicksort.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/sorts/quicksort.py b/sorts/quicksort.py index 1b18fc7135db..7975b7209f8e 100644 --- a/sorts/quicksort.py +++ b/sorts/quicksort.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 sort the lesser and greater lists and concatenate them with the pivot return [*quick_sort(lesser), pivot, *quick_sort(greater)] + if __name__ == "__main__": # Get user input as a list of integers user_input = input("Enter numbers separated by a comma:\n").strip()