Skip to content

Commit 77efa8f

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent 4bb0561 commit 77efa8f

File tree

1 file changed

+9
-11
lines changed

1 file changed

+9
-11
lines changed

divide_and_conquer/quick_sort_with_testing.py

+9-11
Original file line numberDiff line numberDiff line change
@@ -10,46 +10,44 @@ def partition(arr, low, high):
1010
arr[i + 1], arr[high] = arr[high], arr[i + 1]
1111
return i + 1
1212

13+
1314
def quick_sort(arr, low, high):
1415
if low < high:
1516
pi = partition(arr, low, high)
1617
quick_sort(arr, low, pi - 1)
1718
quick_sort(arr, pi + 1, high)
1819

20+
1921
# Function to handle edge cases and testing
2022
def test_quick_sort():
2123
test_cases = [
2224
# Regular case with positive integers
2325
([10, 7, 8, 9, 1, 5], [1, 5, 7, 8, 9, 10]),
24-
2526
# Already sorted array
2627
([1, 2, 3, 4, 5], [1, 2, 3, 4, 5]),
27-
2828
# Reverse sorted array
2929
([5, 4, 3, 2, 1], [1, 2, 3, 4, 5]),
30-
3130
# Array with duplicates
3231
([4, 2, 4, 1, 2, 1], [1, 1, 2, 2, 4, 4]),
33-
3432
# Array with negative and positive numbers
3533
([-3, 5, -1, 7, 0, -2], [-3, -2, -1, 0, 5, 7]),
36-
3734
# Single element array
3835
([42], [42]),
39-
4036
# Empty array
4137
([], []),
42-
4338
# Array with all same elements
44-
([1, 1, 1, 1], [1, 1, 1, 1])
39+
([1, 1, 1, 1], [1, 1, 1, 1]),
4540
]
46-
41+
4742
for i, (arr, expected) in enumerate(test_cases):
4843
print(f"Test case {i+1}: Original array = {arr}")
4944
quick_sort(arr, 0, len(arr) - 1)
50-
assert arr == expected, f"Test case {i+1} failed: Expected {expected}, but got {arr}"
45+
assert (
46+
arr == expected
47+
), f"Test case {i+1} failed: Expected {expected}, but got {arr}"
5148
print(f"Test case {i+1} passed: Sorted array = {arr}")
52-
49+
5350
print("All test cases passed!")
5451

52+
5553
test_quick_sort()

0 commit comments

Comments
 (0)