Skip to content

Update sorts/bubble_sort.py #5802

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 31, 2023

Conversation

dylanbuchi
Copy link
Contributor

@dylanbuchi dylanbuchi commented Nov 10, 2021

Describe your change:

  • In sorts/bubble_sort.py:
    • Add missing type annotations
    • Refactor bubble_sort function
  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Documentation change?

Checklist:

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • All new Python files are placed inside an existing directory.
  • All filenames are in all lowercase characters with no spaces or dashes.
  • All functions and variable names follow Python naming conventions.
  • All function parameters and return values are annotated with Python type hints.
  • All functions have doctests that pass the automated testing.
  • All new algorithms have a URL in its comments that points to Wikipedia or other similar explanation.
  • If this pull request resolves one or more open issues then the commit message contains Fixes: #{$ISSUE_NO}.

@ghost ghost added awaiting reviews This PR is ready to be reviewed enhancement This PR modified some existing files labels Nov 10, 2021
@@ -28,9 +31,9 @@ def bubble_sort(collection):
True
"""
length = len(collection)
for i in range(length - 1):
for i in reversed(range(length)):
Copy link
Member

@l3str4nge l3str4nge Nov 10, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is calling reversed more efficient? I don't think so. Did you do some benchmarks?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reversed has to be a bit better since the Python heapq.py module uses it.
It also improves code readability. @l3str4nge

Source: https://www.python.org/dev/peps/pep-0322/

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know but it would be good to know if algorithms performance is better too :) Could you perform any benchmarks before and after refactor?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmarked with the timeit module:

1000 items:

Before: 
0.10902250000071945

After: 
0.11019620000297436

5000 items:

Before: 
2.8898907999973744

After: 
2.844012000001385

10000 items:

Before: 
12.34897049999563

After: 
11.655540399995516

I tested a bit more locally and sometimes refactored one is better and sometimes it's the original.

@dylanbuchi dylanbuchi requested a review from l3str4nge November 10, 2021 21:48
@stale
Copy link

stale bot commented Apr 19, 2022

This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale Used to mark an issue or pull request stale. label Apr 19, 2022
@stale stale bot removed the stale Used to mark an issue or pull request stale. label Jul 31, 2023
@tianyizheng02 tianyizheng02 merged commit 90a8e6e into TheAlgorithms:master Jul 31, 2023
@algorithms-keeper algorithms-keeper bot removed the awaiting reviews This PR is ready to be reviewed label Jul 31, 2023
sedatguzelsemme pushed a commit to sedatguzelsemme/Python that referenced this pull request Sep 15, 2024
* Add missing type annotations in bubble_sort.py

* Refactor bubble_sort function
@isidroas isidroas mentioned this pull request Jan 25, 2025
14 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement This PR modified some existing files
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants