1
1
import heapq
2
2
import random
3
+
3
4
"""
4
- KKirkpatrick-Reisch sorting algorithm.
5
- Divides input into sqrt(n) blocks, sorts each, then merges using a min-heap.
6
-
7
- Time Complexity:
8
- - Average case: O(n * sqrt(n))
9
- - Worst case: O(n * sqrt(n))
10
- - Best case: O(n * sqrt(n))
11
-
12
- Space Complexity: O(n)
13
-
5
+ Kirkpatrick-Reisch sorting algorithm.
6
+ Divides input into sqrt(n) blocks, sorts each, then merges using a min-heap.
7
+
8
+ Time Complexity:
9
+ - Average case: O(n * sqrt(n))
10
+ - Worst case: O(n * sqrt(n))
11
+ - Best case: O(n * sqrt(n))
12
+
13
+ Space Complexity: O(n)
14
14
"""
15
15
16
+
16
17
def kirkpatrick_reisch_sort (arr ):
18
+ """
19
+ Implements the Kirkpatrick-Reisch sorting algorithm.
20
+
21
+ Args:
22
+ arr (list): The input list to be sorted.
23
+
24
+ Returns:
25
+ list: A new list containing the sorted elements.
26
+
27
+ Examples:
28
+ >>> kirkpatrick_reisch_sort([3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5])
29
+ [1, 1, 2, 3, 3, 4, 5, 5, 5, 6, 9]
30
+
31
+ >>> kirkpatrick_reisch_sort([])
32
+ []
33
+
34
+ >>> kirkpatrick_reisch_sort([1])
35
+ [1]
36
+
37
+ >>> kirkpatrick_reisch_sort([5, 4, 3, 2, 1])
38
+ [1, 2, 3, 4, 5]
39
+
40
+ >>> kirkpatrick_reisch_sort([-1, -3, 5, 0, 2])
41
+ [-3, -1, 0, 2, 5]
42
+ """
17
43
n = len (arr )
18
44
if n <= 1 :
19
45
return arr
@@ -52,4 +78,4 @@ def kirkpatrick_reisch_sort(arr):
52
78
print ("Sorted Array:" , sorted_arr )
53
79
54
80
# Verify the result
55
- assert (sorted_arr == sorted (arr ))
81
+ assert (sorted_arr == sorted (arr ))
0 commit comments