From 7c9b6a81d551a5145af5857aed5f841a8cc0e0fc Mon Sep 17 00:00:00 2001 From: Toshaksha <147024929+Toshaksha@users.noreply.github.com> Date: Thu, 3 Jul 2025 11:48:57 +0530 Subject: [PATCH 1/4] Add quick_sort and 3-way quick_sort with tests --- sorts/quick_sort_with_tests.py | 81 ++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 sorts/quick_sort_with_tests.py diff --git a/sorts/quick_sort_with_tests.py b/sorts/quick_sort_with_tests.py new file mode 100644 index 000000000000..40fbc4c7f0a0 --- /dev/null +++ b/sorts/quick_sort_with_tests.py @@ -0,0 +1,81 @@ +from typing import List, Optional + + +def quick_sort(arr: List[int]) -> List[int]: + """ + Classic quick sort implementation using list comprehensions. + + Args: + arr (List[int]): List of integers to sort. + + Returns: + List[int]: New sorted list. + """ + if len(arr) <= 1: + return arr + else: + pivot = arr[len(arr) // 2] + left = [x for x in arr if x < pivot] + middle = [x for x in arr if x == pivot] + right = [x for x in arr if x > pivot] + return quick_sort(left) + middle + quick_sort(right) + + +def quick_sort_3way(arr: List[int], low: int = 0, high: Optional[int] = None) -> List[int]: + """ + In-place 3-way partitioning quick sort. + + Args: + arr (List[int]): List of integers to sort. + low (int): Starting index of the sublist to sort. + high (Optional[int]): Ending index of the sublist to sort. + + Returns: + List[int]: The same list sorted in-place. + """ + if high is None: + high = len(arr) - 1 + + if low < high: + lt, gt = low, high + pivot = arr[low] + i = low + 1 + + while i <= gt: + if arr[i] < pivot: + arr[lt], arr[i] = arr[i], arr[lt] + lt += 1 + i += 1 + elif arr[i] > pivot: + arr[i], arr[gt] = arr[gt], arr[i] + gt -= 1 + else: + i += 1 + + quick_sort_3way(arr, low, lt - 1) + quick_sort_3way(arr, gt + 1, high) + + return arr + + +def test_quick_sorts(): + """ + Simple test cases for quick_sort and quick_sort_3way functions. + """ + test_cases = [ + ([3, 6, 8, 10, 1, 2, 1], [1, 1, 2, 3, 6, 8, 10]), + ([4, 5, 4, 3, 4, 2, 1, 4], [1, 2, 3, 4, 4, 4, 4, 5]), + ([], []), + ([1], [1]), + ([2, 1], [1, 2]), + ] + + for i, (input_arr, expected) in enumerate(test_cases): + assert quick_sort(input_arr) == expected, f"quick_sort failed on test case {i + 1}" + assert quick_sort_3way(input_arr.copy()) == expected, f"quick_sort_3way failed on test case {i + 1}" + + print("All tests passed!") + + +if __name__ == "__main__": + test_quick_sorts() From 27c8aff67ca4d3053a7c09f2eff4cf45f944d49d Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 3 Jul 2025 06:38:46 +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_with_tests.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/sorts/quick_sort_with_tests.py b/sorts/quick_sort_with_tests.py index 40fbc4c7f0a0..0406f1dae031 100644 --- a/sorts/quick_sort_with_tests.py +++ b/sorts/quick_sort_with_tests.py @@ -21,7 +21,9 @@ def quick_sort(arr: List[int]) -> List[int]: return quick_sort(left) + middle + quick_sort(right) -def quick_sort_3way(arr: List[int], low: int = 0, high: Optional[int] = None) -> List[int]: +def quick_sort_3way( + arr: List[int], low: int = 0, high: Optional[int] = None +) -> List[int]: """ In-place 3-way partitioning quick sort. @@ -71,8 +73,12 @@ def test_quick_sorts(): ] for i, (input_arr, expected) in enumerate(test_cases): - assert quick_sort(input_arr) == expected, f"quick_sort failed on test case {i + 1}" - assert quick_sort_3way(input_arr.copy()) == expected, f"quick_sort_3way failed on test case {i + 1}" + assert quick_sort(input_arr) == expected, ( + f"quick_sort failed on test case {i + 1}" + ) + assert quick_sort_3way(input_arr.copy()) == expected, ( + f"quick_sort_3way failed on test case {i + 1}" + ) print("All tests passed!") From 4cf7f6c23d94fb40a3aaf50637cb96625321205f Mon Sep 17 00:00:00 2001 From: Toshaksha <147024929+Toshaksha@users.noreply.github.com> Date: Thu, 3 Jul 2025 12:28:17 +0530 Subject: [PATCH 3/4] Fix ruff linting --- sorts/quick_sort_with_tests.py | 27 +++++++++------------------ 1 file changed, 9 insertions(+), 18 deletions(-) diff --git a/sorts/quick_sort_with_tests.py b/sorts/quick_sort_with_tests.py index 0406f1dae031..04e644f6f66c 100644 --- a/sorts/quick_sort_with_tests.py +++ b/sorts/quick_sort_with_tests.py @@ -1,15 +1,12 @@ -from typing import List, Optional - - -def quick_sort(arr: List[int]) -> List[int]: +def quick_sort(arr: list[int]) -> list[int]: """ Classic quick sort implementation using list comprehensions. Args: - arr (List[int]): List of integers to sort. + arr (list[int]): List of integers to sort. Returns: - List[int]: New sorted list. + list[int]: New sorted list. """ if len(arr) <= 1: return arr @@ -21,19 +18,17 @@ def quick_sort(arr: List[int]) -> List[int]: return quick_sort(left) + middle + quick_sort(right) -def quick_sort_3way( - arr: List[int], low: int = 0, high: Optional[int] = None -) -> List[int]: +def quick_sort_3way(arr: list[int], low: int = 0, high: int | None = None) -> list[int]: """ In-place 3-way partitioning quick sort. Args: - arr (List[int]): List of integers to sort. + arr (list[int]): List of integers to sort. low (int): Starting index of the sublist to sort. - high (Optional[int]): Ending index of the sublist to sort. + high (int | None): Ending index of the sublist to sort. Returns: - List[int]: The same list sorted in-place. + list[int]: The same list sorted in-place. """ if high is None: high = len(arr) - 1 @@ -73,12 +68,8 @@ def test_quick_sorts(): ] for i, (input_arr, expected) in enumerate(test_cases): - assert quick_sort(input_arr) == expected, ( - f"quick_sort failed on test case {i + 1}" - ) - assert quick_sort_3way(input_arr.copy()) == expected, ( - f"quick_sort_3way failed on test case {i + 1}" - ) + assert quick_sort(input_arr) == expected, f"quick_sort failed on test case {i + 1}" + assert quick_sort_3way(input_arr.copy()) == expected, f"quick_sort_3way failed on test case {i + 1}" print("All tests passed!") From f2676667f6a137e2824fc15aaae0ca01b68039bb Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 3 Jul 2025 06:58:45 +0000 Subject: [PATCH 4/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- sorts/quick_sort_with_tests.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/sorts/quick_sort_with_tests.py b/sorts/quick_sort_with_tests.py index 04e644f6f66c..9d1403edf816 100644 --- a/sorts/quick_sort_with_tests.py +++ b/sorts/quick_sort_with_tests.py @@ -68,8 +68,12 @@ def test_quick_sorts(): ] for i, (input_arr, expected) in enumerate(test_cases): - assert quick_sort(input_arr) == expected, f"quick_sort failed on test case {i + 1}" - assert quick_sort_3way(input_arr.copy()) == expected, f"quick_sort_3way failed on test case {i + 1}" + assert quick_sort(input_arr) == expected, ( + f"quick_sort failed on test case {i + 1}" + ) + assert quick_sort_3way(input_arr.copy()) == expected, ( + f"quick_sort_3way failed on test case {i + 1}" + ) print("All tests passed!")