Skip to content

Commit 431cb38

Browse files
committed
Added doctest
1 parent ca76ce6 commit 431cb38

File tree

1 file changed

+37
-11
lines changed

1 file changed

+37
-11
lines changed

sorts/kirkpatrick_reisch_sort.py

+37-11
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,45 @@
11
import heapq
22
import random
3+
34
"""
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)
1414
"""
1515

16+
1617
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+
"""
1743
n = len(arr)
1844
if n <= 1:
1945
return arr
@@ -52,4 +78,4 @@ def kirkpatrick_reisch_sort(arr):
5278
print("Sorted Array:", sorted_arr)
5379

5480
# Verify the result
55-
assert (sorted_arr == sorted(arr))
81+
assert (sorted_arr == sorted(arr))

0 commit comments

Comments
 (0)