This guideline illustrates the sorting of arrays using the qsort() function in C and also helps you to understand it by C example codes.
qsort() in C
C standard library facilitates us with qsort() function which is simply used to sort arrays. It is an extremely optimized and fast function that can operate with any data type array.
Header-File using qsort() in C
The qsort() method is defined inside the stdlib.h header file, which must be defined before implementing qsort() in a C program.
Declaration of qsort()
The declaration of qsort() function is as follows:
Parameters of qsort() Function
The parameters of the qsort() function are:
base: Pointer to the first element of the array to be sorted.
number_of_elements: Number of elements in the array to be sorted.
size_of_element: Size in bytes of each element in the array.
comparison_function: Pointer to a comparison function that defines the order of the elements.
What is Comparison Function in qsort()
The comparison function takes two parameters, both of type const void*, which point to the elements being compared. The function should return an integer less than, equal to, or greater than 0, depending on whether the first element should be sorted before, at the same position, or after the second element, respectively.
How qsort Works in C
The qsort() function works in the following way:
Note: Here we are considering an int arr[] = {5, 2, 8, 3, 1, 9};
1: Initially, the qsort function will be called with the following parameters:
where arr is the pointer to the array, 6 is the number of elements in the array, sizeof(int) is the size of each element in the array, and comparison_function is the function that determines the order in which the elements are sorted.
2: The qsort function selects a pivot element. Let’s say it selects 3 as the pivot.
3: The qsort function partitions the array into two sub-arrays: {2, 1} and {5, 8, 9}. The first sub-array contains elements that are less than or equal to the pivot, and the second sub-array contains elements that are greater than the pivot.
4: The qsort function recursively calls itself on each of the sub-arrays.
5: The qsort function selects pivots for each of the sub-arrays. Let’s say it selects 1 and 8 as the pivots.
6: The qsort function partitions each sub-array into two more sub-arrays, and recursively calls itself on each of these sub-arrays.
7: The qsort function combines the sorted sub-arrays back into a single sorted array: {1, 2} and {5, 8, 9} become {1, 2, 5, 8, 9}.
8: The entire sorted array is returned.
Implementation of qsort in C Programming
The following code shows the implementation of qsort function in C programming.
#include <stdlib.h>
int compare (const void * a1, const void * b1)
{
return ( *(int*)a1 - *(int*)b1 );
}
int main ()
{
int i = 0, num = 6;
int array[] = {5, 2, 8, 3, 1, 9};
qsort (array, num, sizeof(int), compare);
printf ("Sorted Elements of Array using qsort() are : " );
for (i=0; i < num; i++){
printf ("%d ", array[i]);}
return 0;
}
In the above code, firstly a compare function is made with two parameters a1 and b1. Then execution starts from main(). In the main, we initialize two integer variables as i=0 and num=6. Then we declare an array with six elements as {5, 2, 8, 3, 1, 9}. After this qsort() have three parameters of array type, the num parameter tells the total elements of an array, sizeof(int) refers to the total size of the array, and compare is used to compare array elements one by one. Then print out the sorted array using printf() function in C.
Output
Conclusion
qsort is a powerful function in C programming for sorting arrays of any type. It uses a quicksort algorithm to sort the elements in ascending or descending order based on a comparison function. The above guide shows you an easy way to implement qsort in C programming with a step-by-step working of qsort algorithm.