qsort() Function in C (original) (raw)
Last Updated : 27 Nov, 2024
The **qsort() in C is a library function used to sort an array of items in ascending order or descending order. It stands for "quick sort," as it implements the quicksort algorithm for sorting which is one of the fastest and most efficient algorithms to sort the array.
Let's take a look at an example that sorts an array of integers:
C `
#include <stdio.h> #include <stdlib.h>
int comp(const void *a, const void b) { return ((int *)a - *(int *)b); }
int main() { int arr[] = {5, 2, 3, 1, 4}; int n = sizeof(arr) / sizeof(arr[0]);
// Sort the array arr
qsort(arr, n, sizeof(arr[0]), comp);
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
return 0;
}
`
This article covers the syntax, examples, and rules for defining comparator for qsort() function in C:
Table of Content
**Syntax of qsort()
The qsort() function is defined inside ****<stdlib.h>** header file.
**qsort(arr, n, size, comp);
**Parameters:
- **arr: Pointer to the first element of the array.
- **n: Number of elements in the array.
- **size: Size of each element.
- **comp: Function pointer to a comparison function/comparator.
**Return Value:
- This function does not return any value.
Rules for Defining Comparision Function
The most important part of the qsort() is the comparator function. To provide universal compatibility, the comparator should follow these rules:
- It should accept two const void * arguments.
- It should return integer value:
- **Less than zero (<0): If the first argument should be placed **before the second argument.
- **Zero (0): If both arguments are **equal.
- **Greater than zero (>0): If the first argument should be placed **after the second argument.
**Note: We can use qsort() to sort arrays of any data type, integers, strings and complex structures by typecasting the arguments to the relevant type.
**Examples of qsort()
The below program demonstrates how we can sort a given array using qsort() function in C.
Sort Array of Integers in Descending Order
C `
#include <stdio.h> #include <stdlib.h>
// Comparator to sort in descending order int comp(const void *a, const void b) { return ((int *)b - *(int *)a); }
int main() { int arr[] = {5, 2, 3, 1, 4}; int n = sizeof(arr) / sizeof(arr[0]);
// Sort the array
qsort(arr, n, sizeof(arr[0]), comp);
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
return 0;
}
`
Sort Array of Strings Lexicographically
C `
#include <stdio.h> #include <stdlib.h> #include <string.h>
// Comparison function to sort strings in ascending order int compare(const void *a, const void b) { return strcmp((const char **)a, *(const char **)b); }
int main() { const char *arr[] = {"Welcome", "to", "Geeks", "for", "Geeks", }; int n = sizeof(arr) / sizeof(arr[0]);
// Sort the array of strings
qsort(arr, n, sizeof(arr[0]), compare);
for (int i = 0; i < n; i++)
printf("%s ", arr[i]);
return 0;
}
`
Output
Geeks Geeks Welcome for to
Sort an Array of Structures
C `
#include <stdio.h> #include <stdlib.h>
typedef struct { int data; } A;
// Comparison function to sort by data int comp(const void *a, const void *b) { return ((A *)a)->data - ((A *)b)->data; }
int main() { A arr[] = {5, 2, 3, 1, 4}; int n = sizeof(arr) / sizeof(arr[0]);
// Sort the array of structures
qsort(arr, n, sizeof(A), comp);
for (int i = 0; i < n; i++)
printf("%d ", arr[i].data);
return 0;
}
`