Skip to content

optimize quicksort implementation #11197

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

Closed
wants to merge 2 commits into from
Closed
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
24 changes: 13 additions & 11 deletions sorts/quick_sort.py → sorts/quicksort.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,14 @@
python3 quick_sort.py
"""
from __future__ import annotations

from random import randrange


def quick_sort(collection: list) -> list:
"""A pure Python implementation of quick sort algorithm
"""A pure Python implementation of quicksort algorithm.

:param collection: a mutable collection of comparable items
:return: the same collection ordered by ascending
:return: the same collection ordered in ascending order

Examples:
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
Examples:
# Example 3: Empty list
>>> quick_sort([])
[]
# Example 4: List with a single element
>>> quick_sort([42])
[42]
# Example 5: Already sorted list
>>> quick_sort([1, 2, 3, 4, 5])
[1, 2, 3, 4, 5]
# Example 6: List with duplicate elements
>>> quick_sort([5, 2, 1, 4, 2, 1, 5])
[1, 1, 2, 2, 4, 5, 5]
# Example 7: List with negative and positive numbers
>>> quick_sort([7, -5, 3, 0, -1])
[-5, -1, 0, 3, 7]
# Example 8: List with repeated elements
>>> quick_sort([3, 2, 3, 2, 3, 2])
[2, 2, 2, 3, 3, 3]
# Example 9: List with large values
>>> quick_sort([1000, 999, 10000, 0, -100])
[-100, 0, 999, 1000, 10000]

Copy link
Contributor

Choose a reason for hiding this comment

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

Can you try these tests?

>>> quick_sort([0, 5, 3, 2, 2])
Expand All @@ -26,23 +25,26 @@ def quick_sort(collection: list) -> list:
>>> quick_sort([-2, 5, 0, -45])
[-45, -2, 0, 5]
"""
# Base case: if the collection has 0 or 1 elements, it is already sorted
if len(collection) < 2:
return collection
pivot_index = randrange(len(collection)) # Use random element as pivot
pivot = collection[pivot_index]
greater: list[int] = [] # All elements greater than pivot
lesser: list[int] = [] # All elements less than or equal to pivot

for element in collection[:pivot_index]:
(greater if element > pivot else lesser).append(element)
# Randomly choose a pivot element and remove it from the collection
pivot_index = randrange(len(collection))
pivot = collection.pop(pivot_index)

for element in collection[pivot_index + 1 :]:
(greater if element > pivot else lesser).append(element)
# Partition the remaining elements into two lists: lesser and greater
lesser = [item for item in collection if item <= pivot]
greater = [item for item in collection if item > pivot]

# Recursively sort the lesser and greater lists and concatenate them with the pivot
return [*quick_sort(lesser), pivot, *quick_sort(greater)]


if __name__ == "__main__":
# Get user input as a list of integers
user_input = input("Enter numbers separated by a comma:\n").strip()
unsorted = [int(item) for item in user_input.split(",")]

# Print the result of sorting the input list using quicksort
print(quick_sort(unsorted))