Skip to content

Added complexities #11725

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions sorts/quick_sort.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@ def quick_sort(collection: list) -> list:
[]
>>> quick_sort([-2, 5, 0, -45])
[-45, -2, 0, 5]

Time Complexity:
Best Case: O(n log n)
Average case: O(n log n)
Worst Case: O(n^2)

Space Complexity:
Best Case: O(log n)
Worst Case: O(n)
Comment on lines +35 to +37
Copy link
Contributor

@tianyizheng02 tianyizheng02 Oct 4, 2024

Choose a reason for hiding this comment

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

I'm not sure if this space complexity analysis is accurate for this specific implementation. From Wikipedia, it seems like the $O(\log n)$ space complexity may only hold for a quick sort implementation that performs partitioning in place (though I may be wrong here). This Python implementation doesn't do in-place partitioning.

Copy link
Author

Choose a reason for hiding this comment

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

Yes, the best case is for in place it seems. I rechecked.
Since in place is the best way, should I implement with in-place method?

Copy link
Author

Choose a reason for hiding this comment

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

#11752 In here I have changed the implementation

"""
# Base case: if the collection has 0 or 1 elements, it is already sorted
if len(collection) < 2:
Expand Down