From dc7c49374792a661495960e90ddd9493ff77e69e Mon Sep 17 00:00:00 2001 From: NoodlesIAte <90202919+IAteNoodles@users.noreply.github.com> Date: Fri, 4 Oct 2024 20:20:32 +0530 Subject: [PATCH 1/2] Update quick_sort.py Updated the algorithm to do in-place storage, leading to efficient space management --- sorts/quick_sort.py | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/sorts/quick_sort.py b/sorts/quick_sort.py index 374d52e75c81..adf3ffbbce9f 100644 --- a/sorts/quick_sort.py +++ b/sorts/quick_sort.py @@ -19,6 +19,15 @@ def quick_sort(collection: list) -> list: :param collection: a mutable collection of comparable items :return: the same collection ordered in ascending order + Time Complexity: + Best Case: O(n log n) + Average case: O(n log n) + Worst Case: O(n^2) + + Space Complexity: + Best Case: O(log n) for In-place implementation + Worst Case: O(n) + Examples: >>> quick_sort([0, 5, 3, 2, 2]) [0, 2, 2, 3, 5] @@ -35,10 +44,18 @@ def quick_sort(collection: list) -> list: pivot_index = randrange(len(collection)) pivot = collection.pop(pivot_index) - # 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] + # Initialise empty lists to store the elements + + lesser = [] # Stores elements less than or equal to the pivot + greater = [] # Stores elements greater than the pivot + # Loop through the collections and partition + for item in collection: + if item <= pivot: + lesser.append(item) + else: + greater.append(item) + # Recursively sort the lesser and greater groups, and combine with the pivot return [*quick_sort(lesser), pivot, *quick_sort(greater)] From 5218d6753290556401344a6a4a756a6880782982 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 4 Oct 2024 14:54:18 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- sorts/quick_sort.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/sorts/quick_sort.py b/sorts/quick_sort.py index adf3ffbbce9f..bf927e443edc 100644 --- a/sorts/quick_sort.py +++ b/sorts/quick_sort.py @@ -27,7 +27,7 @@ def quick_sort(collection: list) -> list: Space Complexity: Best Case: O(log n) for In-place implementation Worst Case: O(n) - + Examples: >>> quick_sort([0, 5, 3, 2, 2]) [0, 2, 2, 3, 5] @@ -45,9 +45,9 @@ def quick_sort(collection: list) -> list: pivot = collection.pop(pivot_index) # Initialise empty lists to store the elements - + lesser = [] # Stores elements less than or equal to the pivot - greater = [] # Stores elements greater than the pivot + greater = [] # Stores elements greater than the pivot # Loop through the collections and partition for item in collection: @@ -55,7 +55,7 @@ def quick_sort(collection: list) -> list: lesser.append(item) else: greater.append(item) - + # Recursively sort the lesser and greater groups, and combine with the pivot return [*quick_sort(lesser), pivot, *quick_sort(greater)]