Loading controls...

Quick Sort Settings

Simpler implementation, pivot at end

Simple but can be O(n²) on reverse sorted data

Loading visualization...
Time: O(n log n) avg, O(n²) worst
Space: O(log n)

Legend

Comparing
Elements being compared
Swapping
Elements being swapped
Sorted
Elements in final position
Unsorted
Elements not yet processed
How to read the visualization:
  • • Numbers show element values
  • • Bottom labels show array indices
  • • Colors indicate current operation
  • • Height represents value magnitude

Inspector

No active frame to inspect

Reference implementation of Quick Sort. Select a language tab to view and copy the code.

def quick_sort(arr: list[int], low: int, high: int) -> None:
    if low < high:
        pi = partition(arr, low, high)
        quick_sort(arr, low, pi - 1)
        quick_sort(arr, pi + 1, high)

def partition(arr: list[int], low: int, high: int) -> int:
    pivot = arr[high]
    i = low - 1
    for j in range(low, high):
        if arr[j] <= pivot:
            i += 1
            arr[i], arr[j] = arr[j], arr[i]
    arr[i + 1], arr[high] = arr[high], arr[i + 1]
    return i + 1

Python — Code Walkthrough

  1. Type hints on both functions

    arr: list[int], low: int, high: int) -> None

    Both quick_sort and partition take the same parameters: the list plus the current subarray bounds. Python lists are mutable and passed by reference, so in-place sorting works.

  2. Base case: low < high

    if low < high:

    Recursion stops when the subarray has fewer than 2 elements. Python has no explicit base-case return needed — the if block simply does nothing when low >= high.

  3. Pivot is the last element

    pivot = arr[high]

    The Lomuto scheme always uses the last element as the pivot. The partition rearranges elements so that everything ≤ pivot ends up to its left.

  4. Tuple swap — no temp needed

    arr[i], arr[j] = arr[j], arr[i]

    Python's simultaneous assignment swaps two list elements without a temporary variable. Used both inside the for loop and at the final pivot placement.

  5. Return the pivot index

    return i + 1

    The partition function returns i+1, the index where the pivot was placed. quick_sort uses this to split into two non-overlapping recursive calls.