Open In App

Quick Sort in C

Last Updated : 02 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

QuickSort is one of the best sorting algorithms that follows the divide-and-conquer approach like Merge Sort but unlike Merge Sort, this algorithm does in place sorting. In this article, we will learn how to implement quicksort in C language.

What is QuickSort Algorithm?

The basic idea behind QuickSort is to select a pivot element from the array and partition the other elements into two sub-arrays according to whether they are less than or greater than the pivot. The sub-arrays are then sorted recursively. This process of partitioning and sorting continues until the entire array is sorted.

QuickSort using C Library

C language provides a library function qsort() that allows to you to sort any type of array using custom comparator method.

C
// C Program to sort an array using qsort() function in 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 

Complexity Analysis of Quick Sort

Time Complexity: O(n logn), where n is the size of the array.
Auxiliary Space: O(log n), considering auxiliary stack space.

Implement Your Own Quick Sort in C

In C, the quick sort algorithm basically requires implementation of two functions:

  1. partition() Function
  2. quickSort() Function

The quicksort() function first calls the partition() function. The partition() function rearranges the given subarray around the selected pivot such that all the elements less that pivot are placed left of it and all the elements greater that pivot are placed right of the pivot. Finally, it returns the index of the pivot to the quicksort.

Then quicksort() function divides the array into two subarrays based on this index and repeats the process for these two subarrays. This will go on till the given subarray cannot be further divided. At this point, the array is already sorted.

C Program to Manually Implement Quick Sort

C
// C program to implement 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) {

    // 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
4 7 11 12 13 15 16 18 19 

Time Complexity: O(n logn)
Auxiliary Space: O(log n)


Next Article

Similar Reads