C programming qsort stdlib.h


In C programming the <stdlib.h> qsort function sort the array elements in ascending order.The declaration of the function is given below.

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

Parameters:
base -The first address of the array.

nmemb -The number of elements in the array.

size -Size of each array element in byte.

compar -The comparison function used for sorting the array element.

Return type
void

Description

The ‘qsort’ uses the result returned by the ‘compar’ function to sort the elements in the ascending order.For ‘qsort’ to function properly it requires that the ‘compar’ function operate based on certain rules:

Rule 1: It must return an int type value.

Rule 2: Both the arguments must be void type.

Rule 3:The return value should follow the rule describe below.(consider the two arguments identifier as ‘arg1’ and ‘arg2’).

If arg1 is less than arg2less than zero is returned
If arg1 is equal to arg2zero is returned
If arg1 is greater than arg2value greater than 0 is returned

A code example is provided below which sort the array elements in ascending order,note how the ‘compar’ function is defined.

Code example

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

typedef struct {
int i;
} Magnitude ;

int compar(const void* arg1, const void* arg2)
{
const int first = ((Magnitude*)arg1)->i ; //dereference first arg1

const int second = ((Magnitude*)arg2)->i ; //dereference second arg2

return first – second ;
}

int main( )
{
Magnitude ar[]={ 2 , 45 , 89 , 23 , 9 , 200 };

qsort( ar ,  //The first address of ‘ar’ array
  sizeof(ar)/sizeof( magnituge ) ) ,  //number of elements in ‘ar’ array
  sizeof(Magnitude) ,  //size of each element in ‘ar’ array
compar );  //the comparison function used

printf( “Printing out ‘ar’ elements after sorting \n”);
for (int i = 0; i<(sizeof(ar) / sizeof(Magnitude) ); i++ )
{
printf(“%i “, ar[i].i );
}

getchar( );
return 0;
}

Output

Printing out ‘ar’ elements after sorting
2 9 23 45 89 200

Note,the only purpose of the ‘compar’ function is to return the value 0 , or value less than 0 or value greater than 0 ,based on the comparison of ‘arg1’ and ‘arg2’.And the best way to compare ‘arg1’ and ‘arg2’ is to subtract them and we did just that and the resulting value is used by ‘qsort’.Note here the absolute value of the resultant value of ‘arg1-arg2’ does not matter,what matter is if it is 0 or less than 0 or greater than 0.





string compar function

The ‘compar’ function shown above is meant only for integer type,you can also use it to for an array of floating point type and character type.But for string type the ‘compar’ function should be defined differently.One way of doing it is to use the ‘strcmp’ function to compare the two strings and passed the resultant value to qsort.How it is defined is shown below.

Code example

int compar( const void* arg1, const void* arg2 )
{
return strcmp(*(const char**)arg1 , *(const char **)arg2 );
}

Try using this function to sort string array using the ‘qsort’ function.One example is shown below.Note include the header <string.h> as ‘strcmp’ is found in this header.

Code example

typedef struct {
char c[10];
} String ;

int compar(const void* arg1, const void* arg2)
{
return strcmp( (((String *)arg1)->c) , (((String *)arg2)->c) ); //the ‘strcmp’ function compare two string
}

int main( )
{
String st[]={ “atexit” , “calloc” , “free” , “rand” , “srand” };

qsort( st ,
  5 ,
  sizeof(String) ,
compar );

for (int i = 0; i < (sizeof(st) / sizeof(String)); i++)
{
printf(“%s “,st[i].c);
}

getchar( ) ;
return 0 ;

Output,

atexit calloc free rand srand

The elements are sorted in lexicographical manner.

I am sure defining the ‘compar’ function for string type array is not hard,we just followed the simple rule:return 0 or value less than 0 or value greater than zero.If you have another way to get the required return value without using the ‘strcmp’ function it will work too,because in programming sometimes how does not matter getting the result is more important,but if you care about efficiency or any of the properties that make up a good program then ‘how’ also matters.

If you have any problem or any doubt drop a comment below,in the mean while happy C++ programming 🙂 .


Related Link

->C bsearch stdlib.h