pysorting.quicksort
This module provides an implementation of the Quicksort algorithm, a popular sorting technique. @author: Shashank
Functions
|
Sorts a list of numbers in ascending or descending order using the Quick Sort algorithm. |
Module Contents
- pysorting.quicksort.quick_sort(arr, ascending=True)[source]
Sorts a list of numbers in ascending or descending order using the Quick Sort algorithm.
Quicksort is a divide-and-conquer algorithm that selects a “pivot” element and partitions the array into two sub-arrays: one with elements smaller than the pivot and one with elements greater than the pivot. It recursively sorts the sub-arrays and combines them into a sorted array. The sorting order can be controlled with the ascending parameter.
Parameters:
- arrlist
The list of numeric values to be sorted.
- ascendingbool, optional
If True (default), sorts the list in ascending order. If False, sorts the list in descending order.
Returns:
- list
The sorted array in ascending order if reverse=False, or in descending order if reverse=True.
Raises:
- TypeError
If the input is not a list.
- InvalidElementTypeError
If the list contains non-comparable elements.
- NonUniformTypeError
If the list contains more than one form of data type.
Notes:
This function operates in-place, modifying the input arr directly.
The average time complexity is O(n log n), while the worst-case complexity is O(n^2), which occurs when the pivot selection results in highly unbalanced partitions.
Sorting in descending order is achieved by reversing the comparison logic during partitioning.
Examples:
Sorting in ascending order (default):
>>> quick_sort([4, 2, 7, 1, 3]) [1, 2, 3, 4, 7]
Sorting in descending order:
>>> quick_sort([4, 2, 7, 1, 3], ascending=False) [7, 4, 3, 2, 1]