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

  1. Miladkhoshdel commented on Sep 25, 2026

    @Miladkhoshdel
    ContributorAuthor

    I've opened PR #15435 to fix this by restricting the heap-sort fallback to the active range. It also adds regression tests verifying that elements outside the range remain unchanged.

  2. cclauss commented on Sep 25, 2026

    @cclauss
    Member

    @priya-sundaram-dev Your thoughts?

  3. nicecartoon commented on Sep 26, 2026

    @nicecartoon

    We should make sure PR #15435 updates heap_sort to accept start and

  4. cclauss commented on Sep 26, 2026

    @cclauss
    Member

    Should we revert:

    It would be best if doctests were added to catch this bug and avoid future regressions.

  5. Miladkhoshdel commented on Sep 26, 2026

    @Miladkhoshdel
    ContributorAuthor

    Thanks for the suggestion. I've updated #15435 so heap_sort() accepts start and end, and heapify() accounts for the range offset. The fallback now sorts the selected range directly without creating a temporary list. Existing heap_sort(array) calls remain compatible.

    Added pytest coverage for range boundaries, empty and single-element ranges, and an omitted end bound. All 403 selected tests, 41 existing doctests, and repository-wide pre-commit checks pass.

    Regarding #15432, this fallback already existed when introsort was introduced in #3877, so reverting the tim sort change would not address it.

    For the requested doctest coverage, the contribution guidance discourages changing implementation and doctests together. Would you prefer those examples included in this PR or submitted in a separate test-only follow-up?

  6. cclauss commented on Sep 26, 2026

    @cclauss
    Member

    Please create a separate pr that contains failing tests, so we have proof of the bug(s). Once we have seen those failures, we can review a PR that contains the fixes and those same tests running green this time.

  7. Miladkhoshdel commented on Sep 26, 2026

    @Miladkhoshdel
    ContributorAuthor

    I've opened #15445 with only regression doctests for #15434—no implementation changes.

    The tests cover immediate heap-sort fallback and fallback after partitioning, checking that the requested range is sorted while surrounding elements remain unchanged.

    Locally, the unfixed code produces 48 passing doctest examples and 2 failures. The exact same doctests pass with the implementation from #15435: 50 passed, 0 failed.

    Once you've reviewed the failures in #15445, I'll include these same doctests in #15435.

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