Skip to content

Improve docstrings in sorts/bubble_sort.py to include complexity analysis #14593

Closed
Aditya70-creator/Python
#2

Description

@jgohel9902

Feature description

The docstrings in sorts/bubble_sort.py are missing time and space complexity information, which is present in many other algorithm implementations across the repository.

Suggested improvement:
Add complexity analysis to the existing docstring:

def bubble_sort(collection: list) -> list:
"""
Bubble Sort Algorithm.

Repeatedly steps through the list, compares adjacent elements  and swaps them if they are in the wrong order.

Time Complexity:
    - Best Case: O(n) — already sorted
    - Average Case: O(n²)
    - Worst Case: O(n²)
Space Complexity: O(1) — in-place sorting

Stable: Yes

>>> bubble_sort([4, 3, 2, 1])
[1, 2, 3, 4]
"""

This would make the implementation more educational and consistent with the repository's goal of being a learning resource.

Closes #14610
Closes #14759
Closes #14823
Closes #14842
Closes #14861

Activity

  1. dvanhu commented on May 8, 2026

    @dvanhu

    Hi, I would like to work on this issue. Please assign it to me if available.

  2. sharanabasava-05 commented on May 20, 2026

    @sharanabasava-05

    Hi, I would like to work on this issue . could you assign it to me ?

  3. dhruv-gautam-dev commented on Jun 25, 2026

    @dhruv-gautam-dev

    Hi, I'd like to work on this issue. I'll add the complexity analysis to the docstrings.

  4. cclauss commented on Sep 10, 2026

    @cclauss
    Member

    @priya-sundaram-dev, please review this issue and five open pull requests.

  5. priya-sundaram-dev commented on Sep 10, 2026

    @priya-sundaram-dev
    Contributor

    Triage summary (issue + the 5 open PRs targeting it).

    The issue is already resolved on master. sorts/bubble_sort.py was expanded with full time/space complexity for both functions in #14924 (merged 2026-07-31), and refined again in #15240 (2026-09-09). The current docstrings already document:

    • iterative — O(n) best (early-exit via swapped), O(n²) avg/worst; O(1) auxiliary.
    • recursive — O(n) best, O(n²) avg/worst; O(1) auxiliary for the sort itself, O(n) call-stack frames worst case.

    That best-case nuance matters: bubble_sort_recursive ends with return collection if not swapped else bubble_sort_recursive(collection), so an already-sorted input does a single O(n) pass and stops — best case is O(n), not O(n²).

    All five PRs were opened in May/June 2026, before #14924 merged, and are now superseded — each adds a subset of what master already documents, and none improves on it:

    Recommendation: close #14593 as completed (already fixed by #14924/#15240), and close #14610, #14759, #14823, #14842, #14861 as superseded — with thanks to each contributor for the effort. No merge needed; merging any of them would only re-open the resolved diff (with conflicts against the current PEP 695-typed source) and, for #14610/#14759, introduce an inaccurate best-case claim.

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

    enhancementThis PR modified some existing files

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions