Skip to content

intro_sort heap-sort fallback ignores start and end boundaries #15434

Description

@Miladkhoshdel

Repository commit

b3f113d

Python version (python --version)

Python 3.14.7

Dependencies version (pip freeze)

Not applicable — no third-party dependencies are required. The reproduction uses only Python’s standard library.

Expected behavior

The intro_sort() helper in sorts/intro_sort.py should sort only the
half-open range [start:end], leaving elements outside that range unchanged,
including when it switches to heap sort.

Reproduction:

from sorts.intro_sort import intro_sort

values = [100, 4, 3, 2, 1, -100]
result = intro_sort(values, start=1, end=5, size_threshold=2, max_depth=0)
print(result)

Expected output:

[100, 1, 2, 3, 4, -100]

Only indices 1 through 4 are requested to be sorted. Setting max_depth=0
forces the heap-sort fallback.

Actual behavior

Actual output:

[-100, 1, 2, 3, 4, 100]

The heap-sort fallback calls heap_sort(array) on the entire list, ignoring
start and end. Consequently, elements outside the requested range move.

This report concerns range handling in the intro_sort() helper. It does
not demonstrate incorrect whole-list ordering from the public sort() function.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions