@@ -10,46 +10,44 @@ def partition(arr, low, high):
10
10
arr [i + 1 ], arr [high ] = arr [high ], arr [i + 1 ]
11
11
return i + 1
12
12
13
+
13
14
def quick_sort (arr , low , high ):
14
15
if low < high :
15
16
pi = partition (arr , low , high )
16
17
quick_sort (arr , low , pi - 1 )
17
18
quick_sort (arr , pi + 1 , high )
18
19
20
+
19
21
# Function to handle edge cases and testing
20
22
def test_quick_sort ():
21
23
test_cases = [
22
24
# Regular case with positive integers
23
25
([10 , 7 , 8 , 9 , 1 , 5 ], [1 , 5 , 7 , 8 , 9 , 10 ]),
24
-
25
26
# Already sorted array
26
27
([1 , 2 , 3 , 4 , 5 ], [1 , 2 , 3 , 4 , 5 ]),
27
-
28
28
# Reverse sorted array
29
29
([5 , 4 , 3 , 2 , 1 ], [1 , 2 , 3 , 4 , 5 ]),
30
-
31
30
# Array with duplicates
32
31
([4 , 2 , 4 , 1 , 2 , 1 ], [1 , 1 , 2 , 2 , 4 , 4 ]),
33
-
34
32
# Array with negative and positive numbers
35
33
([- 3 , 5 , - 1 , 7 , 0 , - 2 ], [- 3 , - 2 , - 1 , 0 , 5 , 7 ]),
36
-
37
34
# Single element array
38
35
([42 ], [42 ]),
39
-
40
36
# Empty array
41
37
([], []),
42
-
43
38
# Array with all same elements
44
- ([1 , 1 , 1 , 1 ], [1 , 1 , 1 , 1 ])
39
+ ([1 , 1 , 1 , 1 ], [1 , 1 , 1 , 1 ]),
45
40
]
46
-
41
+
47
42
for i , (arr , expected ) in enumerate (test_cases ):
48
43
print (f"Test case { i + 1 } : Original array = { arr } " )
49
44
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 } "
51
48
print (f"Test case { i + 1 } passed: Sorted array = { arr } " )
52
-
49
+
53
50
print ("All test cases passed!" )
54
51
52
+
55
53
test_quick_sort ()
0 commit comments