Quick Sort in C

Last Updated : 11 Jun, 2026

Quick Sort is a sorting algorithm that arranges elements by repeatedly selecting a pivot and partitioning the array into smaller sections. These sections are then sorted recursively until the entire array is ordered.

  • Sorts the array in-place, requiring only a small amount of extra memory.
  • Achieves an average-case time complexity of O(n log n).
  • Uses recursive partitioning to divide the array into smaller subarrays.

Working of Quick Sort

Quick Sort sorts an array using the following steps:

Quick-Sort-Algorithm

1. Choose a pivot element.

2. Rearrange the array so that:

  • Elements smaller than the pivot appear on its left.
  • Elements greater than the pivot appear on its right.

3. Place the pivot in its correct sorted position.

4. Recursively repeat the same process for the left and right subarrays.

5. Continue until all subarrays contain one or zero elements.

Quick Sort Using the C Standard Library

The C Standard Library provides the qsort() function that can sort arrays of any data type using a user-defined comparison function.

C
#include <stdio.h>
#include <stdlib.h>

// If a should be placed before b, compare function should
// return positive value, if it should be placed after b,
// it should return negative value. Returns 0 otherwise
int compare(const void* a, const void* b) {
    return (*(int*)a - *(int*)b);
}

int main() {
    int arr[] = { 4, 2, 5, 3, 1 };
    int n = sizeof(arr) / sizeof(arr[0]);
	
  	// Sorting arr using inbuilt quicksort method
    qsort(arr, n, sizeof(int), compare);

    for (int i = 0; i < n; i++)
        printf("%d ", arr[i]);

    return 0;
}

Output
1 2 3 4 5 

Explanation

  • The compare() function determines the ordering of elements.
  • qsort() repeatedly uses this function to compare elements and sort the array.
  • After sorting, the elements are printed in ascending order.

Complexity Analysis

  • Time Complexity: O(n log n) (average)
  • Auxiliary Space: O(log n)

Manual Implementation of Quick Sort

A manual implementation of Quick Sort typically uses the following functions:

partition() Function

The partition() function:

  • Selects a pivot element.
  • Rearranges the subarray around the pivot.
  • Places the pivot in its correct position.
  • Returns the final index of the pivot.

quickSort() Function

The quickSort() function:

  • Calls partition() to find the pivot position.
  • Recursively sorts elements on the left side of the pivot.
  • Recursively sorts elements on the right side of the pivot.
  • Stops when the subarray size becomes 0 or 1.
C
#include <stdio.h>

void swap(int* a, int* b) {
    int temp = *a;
    *a = *b;
    *b = temp;
}

int partition(int arr[], int low, int high) {

    // Initialize pivot to be the first element
    int p = arr[low];
    int i = low;
    int j = high;

    while (i < j) {

        // Find the first element greater than
        // the pivot (from starting)
        while (arr[i] <= p && i <= high - 1) {
            i++;
        }

        // Find the first element smaller than
        // the pivot (from last)
        while (arr[j] > p && j >= low + 1) {
            j--;
        }
        if (i < j) {
            swap(&arr[i], &arr[j]);
        }
    }
    swap(&arr[low], &arr[j]);
    return j;
}

void quickSort(int arr[], int low, int high) {
    if (low < high) {

        // call partition function to find Partition Index
        int pi = partition(arr, low, high);

        // Recursively call quickSort() for left and right
        // half based on Partition Index
        quickSort(arr, low, pi - 1);
        quickSort(arr, pi + 1, high);
    }
}

int main() {
  
    int arr[] = { 4, 2, 5, 3, 1 };
    int n = sizeof(arr) / sizeof(arr[0]);

    // calling quickSort() to sort the given array
    quickSort(arr, 0, n - 1);

    for (int i = 0; i < n; i++)
        printf("%d ", arr[i]);

    return 0;
}

Output
1 2 3 4 5 

Explanation

  • The first element of the subarray is selected as the pivot.
  • The partition() function places the pivot in its correct position.
  • Elements smaller than the pivot move to its left.
  • Elements greater than the pivot move to its right.
  • quickSort() recursively sorts both partitions until the entire array is sorted.

Complexity Analysis

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