All algorithms
Sorting

Quick Sort

Partition around a pivot, then recurse — the fastest sort in practice.

TimeO(n log n) averageSpaceO(log n)CategorySorting

Quick Sort picks a pivot and partitions the array so everything smaller sits left of it and everything larger sits right. The pivot is then in its final position, and the two sides are sorted recursively.

In practice it beats Merge Sort: the partition is a tight in-place loop with excellent cache behaviour and no extra buffer. The catch is the worst case — an already-sorted array with a last-element pivot degrades to O(n²), which is why real implementations randomise or use median-of-three.

Partitioning is where learners lose the thread: the `i` pointer marks the end of the 'smaller' region and only advances on a swap. AlgoLens draws both pointers on the array so you can watch the two regions grow.

The code

quick_sort.cpp
#include <bits/stdc++.h>
using namespace std;

vector<int> a = {5, 2, 9, 1, 7, 3};

int partition_(int lo, int hi) {
    int pivot = a[hi];
    int i = lo - 1;
    for (int j = lo; j < hi; j++) {
        if (a[j] <= pivot) {
            i++;
            swap(a[i], a[j]);
        }
    }
    swap(a[i + 1], a[hi]);
    return i + 1;
}

void quickSort(int lo, int hi) {
    if (lo >= hi) return;
    int p = partition_(lo, hi);
    quickSort(lo, p - 1);
    quickSort(p + 1, hi);
}

int main() {
    quickSort(0, a.size() - 1);
    for (int x : a) cout << x << ' ';
    cout << '\n';
    return 0;
}
Open this in the visualizer

Now run your own Quick Sort

AlgoLens traces your code — arrays, graphs, trees and recursion — from a real execution, so you see how it actually behaves, not a canned animation.

Paste your code

Keep going