C++ Program for Quick Sort

Last Updated : 4 Jun, 2026

Quick Sort is an efficient sorting algorithm based on the divide-and-conquer technique. It works by selecting a pivot element, partitioning the array into smaller and larger elements around the pivot, and then recursively sorting the resulting subarrays.

  • Uses the divide-and-conquer strategy.
  • Works efficiently for large datasets.
  • Performs sorting in-place with minimal extra memory.
  • Average-case time complexity is O(n log n).

Working of Quick Sort

Quick Sort follows the divide-and-conquer approach by repeatedly partitioning the array and sorting the smaller subarrays. Quick Sort is typically implemented using two functions:

partition()

The partition() function rearranges the elements around a chosen pivot element.

  • Elements smaller than or equal to the pivot are moved to its left.
  • Elements greater than the pivot are moved to its right.
  • The pivot is placed in its correct sorted position.
  • Returns the partition index of the pivot.

quickSort()

The quickSort() function recursively sorts the array using the divide-and-conquer technique.

  • Calls partition() to find the correct position of the pivot.
  • Recursively sorts the subarray before the pivot.
  • Recursively sorts the subarray after the pivot.
  • Continues until the entire array becomes sorted.
Quick-Sort-Algorithm
Quick Sort

In Above Digram:

  • The pivot element 13 is selected first and placed in its correct sorted position.
  • Elements smaller than or equal to 13 are moved to the left, while larger elements are moved to the right.
  • The left and right subarrays are further partitioned using new pivot elements.
  • This process continues recursively until all elements are sorted.

The following program demonstrates how to implement the Quick Sort algorithm in C++ using recursion and the partitioning technique.

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

int partition(vector<int> &vec, int low, int high) {

    // Selecting last element as the pivot
    int pivot = vec[high];

    // Index of elemment just before the last element
    // It is used for swapping
    int i = (low - 1);

    for (int j = low; j <= high - 1; j++) {

        // If current element is smaller than or
        // equal to pivot
        if (vec[j] <= pivot) {
            i++;
            swap(vec[i], vec[j]);
        }
    }

    // Put pivot to its position
    swap(vec[i + 1], vec[high]);

    // Return the point of partition
    return (i + 1);
}

void quickSort(vector<int> &vec, int low, int high) {

    // Base case: This part will be executed till the starting
    // index low is lesser than the ending index high
    if (low < high) {

        // pi is Partitioning Index, arr[p] is now at
        // right place
        int pi = partition(vec, low, high);

        // Separately sort elements before and after the
        // Partition Index pi
        quickSort(vec, low, pi - 1);
        quickSort(vec, pi + 1, high);
    }
}

int main() {
    vector<int> vec = {19, 17, 15, 12, 16, 18, 4, 11, 13};
    int n = vec.size();
	
  	// Calling quicksort for the vector vec
    quickSort(vec, 0, n - 1);

    for (auto i : vec) {
        cout << i << " ";
    }
    return 0;
}

Output
4 11 12 13 15 16 17 18 19 

Explanation

  • The last element of the vector is chosen as the pivot in the partition() function.
  • Elements smaller than or equal to the pivot are moved to the left side of the array.
  • After partitioning, the pivot is placed in its correct sorted position.
  • quickSort() recursively sorts the elements before and after the pivot.
  • The process continues until all subarrays are sorted, resulting in a fully sorted vector.

Complexity Analysis of Quick Sort

Time Complexity

  • Best Case: O(n log n)
  • Average Case: O(n log n)
  • Worst Case: O(n²)

Auxiliary Space

  • Average Case: O(log n) (recursive call stack)
  • Worst Case: O(n)
Comment