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:

In the Above Diagram
The array shown in the diagram is: {4, 3, 1, 2, 5, 9, 7, 10, 6} The partitioning process shown in the diagram is:
- The first pivot is 5, which divides the array into two subarrays: {4, 3, 1, 2} and {9, 7, 10, 6}.
- The left subarray is partitioned using 2 as the pivot, producing {1} and {4, 3}.
- The {4, 3} subarray is then partitioned using 3 as the pivot.
- The right subarray is partitioned using 6 as the pivot, producing {} and {9, 7, 10}.
- The {9, 7, 10} subarray is further partitioned using 9 and then 7 as pivots.
- The process continues recursively until every element is in its correct position.
The final sorted array is: 1 2 3 4 5 6 7 9 10
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.
#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.
- 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.
#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)