Skip to content

Commit 03b0735

Browse files
test: add failing doctests for introsort heap fallback range boundaries (#15445)
Co-authored-by: Christian Clauss <cclauss@me.com>
1 parent a7a298e commit 03b0735

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
@@ -197,6 +197,26 @@ def intro_sort[T: Comparable](
197197
>>> max_depth = 2 * math.ceil(math.log2(len(array)))
198198
>>> intro_sort(array, 0, len(array), 16, max_depth)
199199
[1, 2, 4, 6, 7, 8, 8, 12, 14, 14, 22, 23, 27, 45, 56, 79]
200+
201+
The heap-sort fallback must preserve elements outside the requested range
202+
and return the original list, even when the depth limit is already reached.
203+
204+
>>> array = [100, *range(40, 0, -1), -100]
205+
>>> expected = [100, *range(1, 41), -100]
206+
>>> result = intro_sort(array, 1, 41, 16, 0)
207+
>>> result is array
208+
True
209+
>>> result == expected
210+
True
211+
212+
The same boundaries must be respected when the fallback occurs after a partition.
213+
214+
>>> array = [100, *range(40, 0, -1), -100]
215+
>>> result = intro_sort(array, 1, 41, 16, 1)
216+
>>> result is array
217+
True
218+
>>> result == expected
219+
True
200220
"""
201221
while end - start > size_threshold:
202222
if max_depth == 0:

0 commit comments

Comments
 (0)