C Programs | IT Developer
IT Developer

C Programs



Share with a Friend

Data Structures in C

Quick Sort

Concept Overview

Quick Sort is a divide-and-conquer algorithm that:

  1. Selects a pivot element.
  2. Partitions the array so that:
    • All elements less than pivot are on the left.
    • All elements greater than pivot are on the right.
  3. Recursively applies the above steps to the left and right subarrays.

Step-by-Step Example

Array:

[8, 3, 1, 7, 0, 10, 2]

  1. Choose a pivot (e.g., last element = 2)
  2. Rearrange elements so that:
    • Left of pivot → smaller
    • Right of pivot → larger
      After partition → [1, 0, 2, 7, 3, 10, 8]
  3. Pivot (2) is now at its correct position.
  4. Recursively sort left [1, 0] and right [7, 3, 10, 8].

 

C Program: Quick Sort

C

#include <stdio.h>

 

// Function to swap two elements

void swap(int *a, int *b) {

    int temp = *a;

    *a = *b;

    *b = temp;

}

 

// Partition function

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

    int pivot = arr[high];  // Choose the last element as pivot

    int i = (low - 1);      // Index of smaller element

 

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

        // If current element is smaller than or equal to pivot

        if (arr[j] <= pivot) {

            i++;

            swap(&arr[i], &arr[j]);

        }

    }

 

    swap(&arr[i + 1], &arr[high]);

    return (i + 1);  // Return the partition index

}

 

// QuickSort function

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

    if (low < high) {

        // Partition the array

        int pi = partition(arr, low, high);

 

        // Recursively sort left and right subarrays

        quickSort(arr, low, pi - 1);

        quickSort(arr, pi + 1, high);

    }

}

 

// Function to print the array

void printArray(int arr[], int size) {

    for (int i = 0; i < size; i++)

        printf("%d ", arr[i]);

    printf("\n");

}

 

int main() {

    int n;

    printf("Enter number of elements: ");

    scanf("%d", &n);

 

    int arr[n];

    printf("Enter %d elements:\n", n);

    for (int i = 0; i < n; i++)

        scanf("%d", &arr[i]);

 

    printf("Original array: ");

    printArray(arr, n);

 

    quickSort(arr, 0, n - 1);

 

    printf("Sorted array: ");

    printArray(arr, n);

 

    return 0;

}

Output

 
INPUT :
Enter number of elements: 7
Enter 7 elements:
8 3 1 7 0 10 2

OUTPUT :
Original array: 8 3 1 7 0 10 2 
Sorted array: 0 1 2 3 7 8 10

Algorithm Summary

Step

Description

1

Choose a pivot element

2

Partition array around pivot

3

Recursively apply to subarrays

4

Combine results (in-place)

Time and Space Complexity

Case

Time Complexity

Space Complexity

Best Case

O(n log n)

O(log n)

Average Case

O(n log n)

O(log n)

Worst Case

O(n²) (when array is already sorted)

O(log n)

Efficient for large datasets
Choose pivot carefully (median or random pivot) to avoid worst case.

Key Takeaways

  • Works in-place (no extra array needed).
  • Very fast for large datasets.
  • Uses divide and conquer
  • Best used when recursion depth is manageable.
  • Not stable (equal elements may change order).