Choose the last item as a pivot, partition smaller values to its left, then recurse on the two sides.

Algorithm

Basic Implementation

basic.cpp
#include <iostream>
#include <vector>

int partition(std::vector<int>& arr, int low, int high) {
    int pivot = arr[high];
    int i = low - 1;
    for (int j = low; j < high; ++j) {
        if (arr[j] <= pivot) {
            ++i;
            std::swap(arr[i], arr[j]);
        }
    }
    std::swap(arr[i + 1], arr[high]);
    return i + 1;
}

void quick_sort(std::vector<int>& arr, int low, int high) {
    if (low < high) {
        int pivot_index = partition(arr, low, high);
        quick_sort(arr, low, pivot_index - 1);
        quick_sort(arr, pivot_index + 1, high);
    }
}

int main() {
    std::vector<int> arr{4, 1, 5, 2, 3};
    quick_sort(arr, 0, static_cast<int>(arr.size()) - 1);
    std::cout << "[";
    for (size_t i = 0; i < arr.size(); ++i) {
        if (i > 0) std::cout << ", ";
        std::cout << arr[i];
    }
    std::cout << "]" << std::endl;
    return 0;
}

The pinned first partition uses [4, 1, 5, 2, 3] with pivot 3. The diagrams track the boundary, swaps, and recursive ranges.

Step 1 - Choose the last value as pivot

The pivot is arr[4] = 3, and i starts just before the current range.

Initial partition state for [4, 1, 5, 2, 3].i0i1i2i3i441523j startspivot

Step 2 - Swap small values left

1 and 2 are <= pivot, so they move into the left partition.

After scanning values before the pivot: [1, 2, 5, 4, 3].i0i1i2i3i412543<= 3<= 3> 3> 3pivot

Step 3 - Place pivot, then recurse

Swapping pivot 3 into index 2 gives [1, 2, 3, 4, 5]; recurse on [1, 2] and [4, 5].

Pivot lands at index 2 and splits the remaining work.left rangepivotright range[1, 2]3 at i2[4, 5]quick_sort(0,1)fixedquick_sort(3,4)

Complexity

  • Time: O(n^2) worst, O(n log n) average
  • Space: O(log n) average call stack
  • Stable: no

Implementation notes

  • In C++, the input is a std::vector<int> initialized as {4, 1, 5, 2, 3} and passed by non-const reference to both quick_sort(std::vector<int>& arr, int low, int high) and partition.
  • Bounds are int indexes. The initial call uses static_cast<int>(arr.size()) - 1, and recursion only continues while low < high.
  • Lomuto partition chooses int pivot = arr[high], starts i = low - 1, and scans j from low to high - 1. Values <= pivot increment i and swap into the left partition.
  • Swaps use std::swap(arr[i], arr[j]) and the final std::swap(arr[i + 1], arr[high]); no temporary buffer is allocated.
  • The trace shows pivot 3: 1 and 2 move left, 4 and 5 stay right, and the final pivot swap changes [1, 2, 5, 4, 3] to [1, 2, 3, 4, 5].
  • Output is streamed with std::cout, a size_t print loop, comma separators, and std::endl. Visible dynamic storage is the input vector; mutation is vector element swapping and scalar partition state across recursive calls.
pivot The final element is moved to the boundary between smaller and larger values.
partition One scan rearranges the current range before the recursive calls.