From 13142f169d87fcf92471d5b45c7e56744f6222aa Mon Sep 17 00:00:00 2001 From: Akhileshkulal Date: Fri, 27 Mar 2026 20:14:17 +0530 Subject: [PATCH 1/2] Improve quick_sort to avoid mutating input list --- sorts/quick_sort.py | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/sorts/quick_sort.py b/sorts/quick_sort.py index 374d52e75c81..ed6cebe5770b 100644 --- a/sorts/quick_sort.py +++ b/sorts/quick_sort.py @@ -9,15 +9,15 @@ """ from __future__ import annotations - from random import randrange def quick_sort(collection: list) -> list: - """A pure Python implementation of quicksort algorithm. + """Return a new list containing the sorted elements of the collection + using the Quick Sort algorithm. - :param collection: a mutable collection of comparable items - :return: the same collection ordered in ascending order + :param collection: a list of comparable items + :return: a new list sorted in ascending order Examples: >>> quick_sort([0, 5, 3, 2, 2]) @@ -27,19 +27,23 @@ 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 + + # Base case: if list has 0 or 1 element it is already sorted if len(collection) < 2: - return collection + return collection.copy() - # Randomly select a pivot index and remove the pivot element from the collection + # Select a random pivot pivot_index = randrange(len(collection)) - pivot = collection.pop(pivot_index) + pivot = collection[pivot_index] - # Partition the remaining elements into two groups: lesser or equal, and greater - lesser = [item for item in collection if item <= pivot] + # Partition the elements + lesser = [ + item for index, item in enumerate(collection) + if item <= pivot and index != pivot_index + ] greater = [item for item in collection if item > pivot] - # Recursively sort the lesser and greater groups, and combine with the pivot + # Recursively sort both partitions return [*quick_sort(lesser), pivot, *quick_sort(greater)] @@ -48,5 +52,5 @@ def quick_sort(collection: list) -> list: 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)) + # Print the sorted result + print(quick_sort(unsorted)) \ No newline at end of file From 58097f66ffd1763036850d98e2683764589b3d6f Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 27 Mar 2026 14:45:38 +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 | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/sorts/quick_sort.py b/sorts/quick_sort.py index ed6cebe5770b..c5de802fa54b 100644 --- a/sorts/quick_sort.py +++ b/sorts/quick_sort.py @@ -38,7 +38,8 @@ def quick_sort(collection: list) -> list: # Partition the elements lesser = [ - item for index, item in enumerate(collection) + item + for index, item in enumerate(collection) if item <= pivot and index != pivot_index ] greater = [item for item in collection if item > pivot] @@ -53,4 +54,4 @@ def quick_sort(collection: list) -> list: unsorted = [int(item) for item in user_input.split(",")] # Print the sorted result - print(quick_sort(unsorted)) \ No newline at end of file + print(quick_sort(unsorted))