a pure python implementation of the quick sort algorithm for doctests run following command python3 m doctest v quicksort py for manual testing run python3 quicksort py a pure python implementation of quick sort algorithm param collection a mutable collection of comparable items return the same collection ordered by ascending examples quicksort0 5 3 2 2 0 2 2 3 5 quicksort quicksort2 5 0 45 45 2 0 5 a pure python implementation of quick sort algorithm param collection a mutable collection of comparable items return the same collection ordered by ascending examples quick_sort 0 5 3 2 2 0 2 2 3 5 quick_sort quick_sort 2 5 0 45 45 2 0 5 use random element as pivot all elements greater than pivot all elements less than or equal to pivot
from __future__ import annotations

from random import randrange


def quick_sort(collection: list) -> list:
    
    if len(collection) < 2:
        return collection
    pivot_index = randrange(len(collection))  
    pivot = collection[pivot_index]
    greater: list[int] = []  
    lesser: list[int] = []  

    for element in collection[:pivot_index]:
        (greater if element > pivot else lesser).append(element)

    for element in collection[pivot_index + 1 :]:
        (greater if element > pivot else lesser).append(element)

    return [*quick_sort(lesser), pivot, *quick_sort(greater)]


if __name__ == "__main__":
    user_input = input("Enter numbers separated by a comma:\n").strip()
    unsorted = [int(item) for item in user_input.split(",")]
    print(quick_sort(unsorted))