Explain the quick sort technique in C language.

Quick sort is a highly efficient divide-and-conquer sorting algorithm that works by selecting a 'pivot' element and partitioning the array around it. Elements smaller than the pivot go to the left, and larger elements go to the right, then the process is repeated recursively on both subarrays.

How Quick Sort Works

The quick sort algorithm follows these steps −

  • Step 1: Choose a pivot element from the array (typically the first, last, or middle element)
  • Step 2: Partition the array so elements less than pivot are on the left, greater elements on the right
  • Step 3: Recursively apply quick sort to the left and right subarrays
  • Step 4: Combine the results to get the final sorted array

Syntax

void quickSort(int arr[], int low, int high);
int partition(int arr[], int low, int high);
void swap(int* a, int* b);

Quick Sort Algorithm Visualization

Quick Sort Process Initial Array: 6 3 7 2 4 5 Pivot = 6 After Partition: 3 2 4 5 6 7 Elements < 6 Pivot Elements > 6 Final Sorted: 2 3 4 5 6 7

Example: Quick Sort Implementation

Here is a complete C program that implements quick sort algorithm −

#include <stdio.h>

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

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

void quickSort(int arr[], int low, int high) {
    if (low < high) {
        int pi = partition(arr, low, high);
        quickSort(arr, low, pi - 1);
        quickSort(arr, pi + 1, high);
    }
}

void printArray(int arr[], int size) {
    for (int i = 0; i < size; i++) {
        printf("%d ", arr[i]);
    }
    printf("<br>");
}

int main() {
    int arr[] = {64, 34, 25, 12, 22, 11, 90};
    int n = sizeof(arr) / sizeof(arr[0]);
    
    printf("Original array: ");
    printArray(arr, n);
    
    quickSort(arr, 0, n - 1);
    
    printf("Sorted array: ");
    printArray(arr, n);
    
    return 0;
}
Original array: 64 34 25 12 22 11 90 
Sorted array: 11 12 22 25 34 64 90 

Time and Space Complexity

Case Time Complexity Description
Best Case O(n log n) When pivot divides array into equal halves
Average Case O(n log n) Random distribution of elements
Worst Case O(n²) When pivot is always smallest or largest element
Space Complexity O(log n) Due to recursive function calls

Key Points

  • Quick sort is an in-place sorting algorithm that doesn't require extra memory
  • It performs well on average but has worst-case O(n²) time complexity
  • The choice of pivot greatly affects performance
  • It's widely used due to its efficiency and simplicity

Conclusion

Quick sort is one of the most efficient sorting algorithms with average O(n log n) time complexity. Its divide-and-conquer approach and in-place sorting make it ideal for most practical applications, though care should be taken in pivot selection to avoid worst-case scenarios.

Updated on: 2026-03-15T13:58:50+05:30

95K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements