DSA
    intermediate

    Quick Sort

    Quick Sort Algorithm

    O(n log n) avg
    Partition

    Quick Sort: Level 0 (partition)

    Starting Quick Sort with 7 elements

    29
    [0]
    10
    [1]
    14
    [2]
    37
    [3]
    13
    [4]
    8
    [5]
    25
    [6]
    Unsorted
    Current Range
    Pivot
    Left Pointer
    Sorted
    Step
    1
    Level
    0
    Phase
    partition
    Comparisons
    0
    Swaps
    0

    Current array: [29, 10, 14, 37, 13, 8, 25] (length: 7)

    Partition around a pivot

    Sequence

    Order one level of quick sort (Lomuto partition).

    Tap a piece, then tap where it goes — or drag it. Tap the tray to take one back.

    1. 1Drop a step here
    2. 2Drop a step here
    3. 3Drop a step here
    4. 4Drop a step here
    5. 5Drop a step here
    5 pieces left to place

    Test your knowledge

    20 questions
    Question 1 of 20
    Score: 0

    What is the "Partition" step in Quick Sort?

    The short version

    Imagine a teacher lining students up by height. She picks one student (the pivot) and says: 'Shorter to the left, taller to the right.' Now she has two smaller groups and repeats. Much faster than moving one student at a time!

    In more depth

    Partitioning is Key

    Quick Sort revolves around the idea of Partitioning. By picking a "pivot" element and organizing the array so that everything smaller is on the left and everything larger is on the right, we effectively place the pivot in its final, correct position.

    Why is it so fast?

    While its worst-case time complexity is O(n²), its average case is O(n log n) with very small constant factors. It sorts in-place (using little extra memory) and is cache-friendly, often outperforming Merge Sort and Heap Sort in practice on arrays.

    Watch out for:

    The choice of pivot matters! Picking the first or last element in a sorted array leads to the dreaded O(n²) worst case. Randomized pivots or "Median of Three" helps avoid this.

    When to reach for this

    • General purpose sorting (standard library implementations often use it)
    • When **Space Complexity** matters (O(log n) stack space vs O(n) for Merge Sort)
    • When you don't need a Stable Sort

    Interview Questions

    Google
    Amazon
    Microsoft
    Apple
    Facebook
    Uber
    Amazon
    Bloomberg
    Microsoft
    Google
    LinkedIn

    Love this visualization?

    Sign up to track your progress, earn achievements, use the AI assistant, and access your personal learning dashboard.

    100% free • No credit card required