![]() |
VOOZH | about |
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:
1 2 3 4 5
This article covers the syntax, examples, and rules for defining comparator for qsort() function in C:
Table of Content
The qsort() function is defined inside <stdlib.h> header file.
qsort(arr, n, size, comp);
Parameters:
Return Value:
The most important part of the qsort() is the comparator function. To provide universal compatibility, the comparator should follow these rules:
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.
The below program demonstrates how we can sort a given array using qsort() function in C.
5 4 3 2 1
Geeks Geeks Welcome for to
1 2 3 4 5