Skip to content

Commit 31c34cd

Browse files
committed
test: add failing doctests for introsort heap fallback range boundaries
1 parent 78e489f commit 31c34cd

1 file changed

Lines changed: 20 additions & 0 deletions

File tree

‎sorts/intro_sort.py‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,26 @@ def intro_sort[T: Comparable](
184184
>>> max_depth = 2 * math.ceil(math.log2(len(array)))
185185
>>> intro_sort(array, 0, len(array), 16, max_depth)
186186
[1, 2, 4, 6, 7, 8, 8, 12, 14, 14, 22, 23, 27, 45, 56, 79]
187+
188+
The heap-sort fallback must preserve elements outside the requested range
189+
and return the original list, even when the depth limit is already reached.
190+
191+
>>> array = [100, *range(40, 0, -1), -100]
192+
>>> expected = [100, *range(1, 41), -100]
193+
>>> result = intro_sort(array, 1, 41, 16, 0)
194+
>>> result is array
195+
True
196+
>>> result == expected
197+
True
198+
199+
The same boundaries must be respected when the fallback occurs after a partition.
200+
201+
>>> array = [100, *range(40, 0, -1), -100]
202+
>>> result = intro_sort(array, 1, 41, 16, 1)
203+
>>> result is array
204+
True
205+
>>> result == expected
206+
True
187207
"""
188208
while end - start > size_threshold:
189209
if max_depth == 0:

0 commit comments

Comments
 (0)