@@ -66,6 +66,13 @@ def _generate_leonardo_numbers(max_value: int) -> list[int]:
6666 """
6767 Generate Leonardo numbers up to max_value.
6868 L(0) = 1, L(1) = 1, L(n) = L(n-1) + L(n-2) + 1
69+
70+ >>> _generate_leonardo_numbers(10)
71+ [1, 1, 3, 5, 9, 15]
72+ >>> _generate_leonardo_numbers(2)
73+ [1, 1, 3]
74+ >>> _generate_leonardo_numbers(0)
75+ [1, 1]
6976 """
7077 leonardo = [1 , 1 ]
7178 while leonardo [- 1 ] < max_value :
@@ -74,26 +81,59 @@ def _generate_leonardo_numbers(max_value: int) -> list[int]:
7481
7582
7683def _smooth_sort_build (arr : list [int ], leonardo : list [int ]) -> None :
77- """Build the Leonardo heap forest."""
84+ """
85+ Build the Leonardo heap forest.
86+
87+ >>> arr = [3, 1, 2]
88+ >>> leo = _generate_leonardo_numbers(len(arr))
89+ >>> _smooth_sort_build(arr, leo)
90+ >>> arr # Array is partially heapified
91+ [3, 1, 2]
92+ """
7893 for i in range (len (arr )):
7994 _add_to_heap (arr , i , leonardo )
8095
8196
8297def _smooth_sort_extract (arr : list [int ], leonardo : list [int ]) -> None :
83- """Extract elements to produce sorted output."""
98+ """
99+ Extract elements to produce sorted output.
100+
101+ >>> arr = [3, 2, 1]
102+ >>> leo = _generate_leonardo_numbers(len(arr))
103+ >>> _smooth_sort_build(arr, leo)
104+ >>> _smooth_sort_extract(arr, leo)
105+ >>> arr
106+ [1, 2, 3]
107+ """
84108 for i in range (len (arr ) - 1 , 0 , - 1 ):
85109 _extract_from_heap (arr , i , leonardo )
86110
87111
88112def _add_to_heap (arr : list [int ], end : int , leonardo : list [int ]) -> None :
89- """Add element at index 'end' to the Leonardo heap."""
113+ """
114+ Add element at index 'end' to the Leonardo heap.
115+
116+ >>> arr = [1, 3, 2]
117+ >>> leo = _generate_leonardo_numbers(len(arr))
118+ >>> _add_to_heap(arr, 2, leo)
119+ >>> arr
120+ [2, 3, 1]
121+ """
90122 # This is a simplified version that focuses on correctness
91123 # We use a basic approach: maintain heap property up to 'end'
92124 _heapify_up (arr , end , leonardo )
93125
94126
95127def _extract_from_heap (arr : list [int ], end : int , leonardo : list [int ]) -> None :
96- """Remove maximum element from heap ending at index 'end'."""
128+ """
129+ Remove maximum element from heap ending at index 'end'.
130+
131+ >>> arr = [5, 3, 4, 1, 2]
132+ >>> leo = _generate_leonardo_numbers(len(arr))
133+ >>> _extract_from_heap(arr, 4, leo)
134+ >>> arr[4] # Maximum moved to end
135+ 5
136+ """
97137 # Find maximum in the range [0, end] and swap it to position 'end'
98138 max_idx = 0
99139 for i in range (1 , end + 1 ):
@@ -107,7 +147,15 @@ def _extract_from_heap(arr: list[int], end: int, leonardo: list[int]) -> None:
107147
108148
109149def _heapify_up (arr : list [int ], index : int , leonardo : list [int ]) -> None :
110- """Restore heap property from bottom up."""
150+ """
151+ Restore heap property from bottom up.
152+
153+ >>> arr = [1, 2, 5, 3]
154+ >>> leo = _generate_leonardo_numbers(len(arr))
155+ >>> _heapify_up(arr, 2, leo)
156+ >>> arr
157+ [5, 2, 1, 3]
158+ """
111159 while index > 0 :
112160 # Find parent using Leonardo number structure
113161 parent = _find_parent (index , leonardo )
@@ -119,7 +167,14 @@ def _heapify_up(arr: list[int], index: int, leonardo: list[int]) -> None:
119167
120168
121169def _heapify_down (arr : list [int ], index : int , end : int ) -> None :
122- """Restore heap property from top down."""
170+ """
171+ Restore heap property from top down.
172+
173+ >>> arr = [1, 5, 3, 2, 4]
174+ >>> _heapify_down(arr, 0, 4)
175+ >>> arr
176+ [5, 4, 3, 2, 1]
177+ """
123178 while index < end :
124179 # Find children
125180 left = 2 * index + 1
@@ -139,7 +194,19 @@ def _heapify_down(arr: list[int], index: int, end: int) -> None:
139194
140195
141196def _find_parent (index : int , leonardo : list [int ]) -> int :
142- """Find parent index in Leonardo heap structure."""
197+ """
198+ Find parent index in Leonardo heap structure.
199+
200+ >>> leo = _generate_leonardo_numbers(10)
201+ >>> _find_parent(0, leo)
202+ -1
203+ >>> _find_parent(1, leo)
204+ 0
205+ >>> _find_parent(2, leo)
206+ 0
207+ >>> _find_parent(5, leo)
208+ 2
209+ """
143210 if index <= 0 :
144211 return - 1
145212
0 commit comments