0
0
CHow-ToBeginner · 3 min read

How to Sort an Array in C: Simple Guide with Example

To sort an array in C, use the qsort function from stdlib.h. You provide the array, number of elements, size of each element, and a comparison function that defines the sorting order.
📐

Syntax

The qsort function sorts an array in place. Its syntax is:

void qsort(void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *));
  • base: pointer to the first element of the array
  • nmemb: number of elements in the array
  • size: size in bytes of each element
  • compar: pointer to a function that compares two elements

The comparison function returns a negative, zero, or positive value depending on the order.

c
void qsort(void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *));
💻

Example

This example sorts an integer array in ascending order using qsort. It shows how to write the comparison function and call qsort.

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

// Comparison function for integers
int compare_ints(const void *a, const void *b) {
    int arg1 = *(const int *)a;
    int arg2 = *(const int *)b;
    return (arg1 > arg2) - (arg1 < arg2); // returns positive if arg1>arg2, negative if less, 0 if equal
}

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

    qsort(arr, n, sizeof(int), compare_ints);

    printf("Sorted array: ");
    for (int i = 0; i < n; i++) {
        printf("%d ", arr[i]);
    }
    printf("\n");
    return 0;
}
Output
Sorted array: 1 2 5 5 6 9
⚠️

Common Pitfalls

  • Not writing the comparison function correctly can cause wrong sorting or crashes.
  • Passing wrong element size or number of elements leads to undefined behavior.
  • For descending order, reverse the comparison logic.
  • Remember qsort sorts the original array; it does not return a new array.
c
#include <stdio.h>
#include <stdlib.h>

// Wrong comparison: returns only 1 or 0, not negative/positive
int wrong_compare(const void *a, const void *b) {
    int arg1 = *(const int *)a;
    int arg2 = *(const int *)b;
    return arg1 > arg2 ? 1 : (arg1 < arg2 ? -1 : 0); // FIXED to return negative, zero, or positive
}

// Correct comparison for descending order
int compare_desc(const void *a, const void *b) {
    int arg1 = *(const int *)a;
    int arg2 = *(const int *)b;
    return (arg2 > arg1) - (arg2 < arg1);
}

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

    // qsort(arr, n, sizeof(int), wrong_compare); // This causes wrong sorting
    qsort(arr, n, sizeof(int), compare_desc); // Correct descending sort

    for (int i = 0; i < n; i++) {
        printf("%d ", arr[i]);
    }
    printf("\n");
    return 0;
}
Output
4 3 1
📊

Quick Reference

Remember these key points when sorting arrays in C:

  • Use qsort from stdlib.h.
  • Write a comparison function returning negative, zero, or positive.
  • Pass correct array pointer, element count, and element size.
  • qsort sorts in place; no new array is created.

Key Takeaways

Use the standard qsort function to sort arrays in C efficiently.
Write a proper comparison function that returns negative, zero, or positive values.
Always pass the correct element size and number of elements to qsort.
qsort sorts the original array in place; it does not create a new array.
Test your comparison function carefully to avoid incorrect sorting results.