From 4bb056170f0e53f82d52c35c7ee95bfc8b31efa3 Mon Sep 17 00:00:00 2001 From: Prathamesh Kusalkar <88785845+KPrathamesh-27@users.noreply.github.com> Date: Mon, 14 Oct 2024 18:51:56 +0530 Subject: [PATCH 1/2] Create quick_sort_with_testing.py Implemented the quick sort and also included the test cases to check for the edge case where it satisfies the algorithm. --- divide_and_conquer/quick_sort_with_testing.py | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 divide_and_conquer/quick_sort_with_testing.py diff --git a/divide_and_conquer/quick_sort_with_testing.py b/divide_and_conquer/quick_sort_with_testing.py new file mode 100644 index 000000000..7e1e46b10 --- /dev/null +++ b/divide_and_conquer/quick_sort_with_testing.py @@ -0,0 +1,55 @@ +def partition(arr, low, high): + pivot = arr[high] + i = low - 1 + + for j in range(low, high): + if arr[j] <= pivot: + i += 1 + arr[i], arr[j] = arr[j], arr[i] + + arr[i + 1], arr[high] = arr[high], arr[i + 1] + return i + 1 + +def quick_sort(arr, low, high): + if low < high: + pi = partition(arr, low, high) + quick_sort(arr, low, pi - 1) + quick_sort(arr, pi + 1, high) + +# Function to handle edge cases and testing +def test_quick_sort(): + test_cases = [ + # Regular case with positive integers + ([10, 7, 8, 9, 1, 5], [1, 5, 7, 8, 9, 10]), + + # Already sorted array + ([1, 2, 3, 4, 5], [1, 2, 3, 4, 5]), + + # Reverse sorted array + ([5, 4, 3, 2, 1], [1, 2, 3, 4, 5]), + + # Array with duplicates + ([4, 2, 4, 1, 2, 1], [1, 1, 2, 2, 4, 4]), + + # Array with negative and positive numbers + ([-3, 5, -1, 7, 0, -2], [-3, -2, -1, 0, 5, 7]), + + # Single element array + ([42], [42]), + + # Empty array + ([], []), + + # Array with all same elements + ([1, 1, 1, 1], [1, 1, 1, 1]) + ] + + for i, (arr, expected) in enumerate(test_cases): + print(f"Test case {i+1}: Original array = {arr}") + quick_sort(arr, 0, len(arr) - 1) + assert arr == expected, f"Test case {i+1} failed: Expected {expected}, but got {arr}" + print(f"Test case {i+1} passed: Sorted array = {arr}") + + print("All test cases passed!") + +test_quick_sort() From 77efa8fa7cbd2eed895f362fdd88c2a88c6496bd Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 14 Oct 2024 13:27:37 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- divide_and_conquer/quick_sort_with_testing.py | 20 +++++++++---------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/divide_and_conquer/quick_sort_with_testing.py b/divide_and_conquer/quick_sort_with_testing.py index 7e1e46b10..e428f0c80 100644 --- a/divide_and_conquer/quick_sort_with_testing.py +++ b/divide_and_conquer/quick_sort_with_testing.py @@ -10,46 +10,44 @@ def partition(arr, low, high): arr[i + 1], arr[high] = arr[high], arr[i + 1] return i + 1 + def quick_sort(arr, low, high): if low < high: pi = partition(arr, low, high) quick_sort(arr, low, pi - 1) quick_sort(arr, pi + 1, high) + # Function to handle edge cases and testing def test_quick_sort(): test_cases = [ # Regular case with positive integers ([10, 7, 8, 9, 1, 5], [1, 5, 7, 8, 9, 10]), - # Already sorted array ([1, 2, 3, 4, 5], [1, 2, 3, 4, 5]), - # Reverse sorted array ([5, 4, 3, 2, 1], [1, 2, 3, 4, 5]), - # Array with duplicates ([4, 2, 4, 1, 2, 1], [1, 1, 2, 2, 4, 4]), - # Array with negative and positive numbers ([-3, 5, -1, 7, 0, -2], [-3, -2, -1, 0, 5, 7]), - # Single element array ([42], [42]), - # Empty array ([], []), - # Array with all same elements - ([1, 1, 1, 1], [1, 1, 1, 1]) + ([1, 1, 1, 1], [1, 1, 1, 1]), ] - + for i, (arr, expected) in enumerate(test_cases): print(f"Test case {i+1}: Original array = {arr}") quick_sort(arr, 0, len(arr) - 1) - assert arr == expected, f"Test case {i+1} failed: Expected {expected}, but got {arr}" + assert ( + arr == expected + ), f"Test case {i+1} failed: Expected {expected}, but got {arr}" print(f"Test case {i+1} passed: Sorted array = {arr}") - + print("All test cases passed!") + test_quick_sort()