Skip to main content

Quick Sort

Quick Sort is a divide-and-conquer sorting algorithm. It selects a 'pivot' element and partitions the array such that elements smaller than the pivot go to the left, and larger ones go to the right, before recursively sorting the sub-arrays. Highly efficient in-place sorting utility.

Complexity Profile

CaseComplexity
Best CaseO(N log N)
Average CaseO(N log N)
Worst CaseO(N^2)
Space ComplexityO(log N)

Code Implementation

def quick_sort(arr):
if len(arr) <= 1:
return arr
pivot = arr[len(arr) // 2]
left = [x for x in arr if x < pivot]
middle = [x for x in arr if x == pivot]
right = [x for x in arr if x > pivot]
return quick_sort(left) + middle + quick_sort(right)

Real-World Applications

  • In-place sorting library functions (e.g. C standard library qsort).
  • Embedded microchips with highly restrictive RAM specifications.
  • Real-time rendering systems where sorting speed is critical.