Skip to content
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
31 changes: 18 additions & 13 deletions sorts/quick_sort.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@
python3 quick_sort.py
"""

from __future__ import annotations

from random import randrange

Check failure on line 12 in sorts/quick_sort.py

View workflow job for this annotation

GitHub Actions / ruff

ruff (I001)

sorts/quick_sort.py:11:1: I001 Import block is un-sorted or un-formatted help: Organize imports

Check failure on line 12 in sorts/quick_sort.py

View workflow job for this annotation

GitHub Actions / ruff

ruff (I001)

sorts/quick_sort.py:11:1: I001 Import block is un-sorted or un-formatted help: Organize imports


def quick_sort(collection: list) -> list:
"""A pure Python implementation of quicksort algorithm.
"""Return a new list containing the sorted elements of the collection
using the Quick Sort algorithm.

:param collection: a mutable collection of comparable items
:return: the same collection ordered in ascending order
:param collection: a list of comparable items
:return: a new list sorted in ascending order

Examples:
>>> quick_sort([0, 5, 3, 2, 2])
Expand All @@ -27,19 +27,24 @@
>>> quick_sort([-2, 5, 0, -45])
[-45, -2, 0, 5]
"""
# Base case: if the collection has 0 or 1 elements, it is already sorted

# Base case: if list has 0 or 1 element it is already sorted
if len(collection) < 2:
return collection
return collection.copy()

# Randomly select a pivot index and remove the pivot element from the collection
# Select a random pivot
pivot_index = randrange(len(collection))
pivot = collection.pop(pivot_index)

# Partition the remaining elements into two groups: lesser or equal, and greater
lesser = [item for item in collection if item <= pivot]
pivot = collection[pivot_index]

# Partition the elements
lesser = [
item
for index, item in enumerate(collection)
if item <= pivot and index != pivot_index
]
greater = [item for item in collection if item > pivot]

# Recursively sort the lesser and greater groups, and combine with the pivot
# Recursively sort both partitions
return [*quick_sort(lesser), pivot, *quick_sort(greater)]


Expand All @@ -48,5 +53,5 @@
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 user-provided list
# Print the sorted result
print(quick_sort(unsorted))
Loading