C++ cstdlib qsort


The C++ <cstdlib> 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 <iostream>
#include <cstdlib>

using namespace std;

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

const int second = *( (const int*) arg2); // dereference second arg2

return (first – second) ;
}

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

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

cout<< “Printing out ‘ar’ elements after sorting “;
for( auto elem:ar )
{
cout<< elem ;
}

cin.get( );
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.

In the ‘for’ statement the code used is ‘auto elem:ar‘ such method of accessing the array element is known as ranged-based for loop,it was introduced in C++11.It is a more efficient way to access elements in any container.

Link : C++11 range-based for loop





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 <cstring> as ‘strcmp’ is found in this header.

Code example

int main( )
{
char *st[]={ “new” , “delete” , “using” , “=default” , “=delete” };

qsort( st ,
  5 ,
  sizeof(char*) ,
compar );

cout<< “Printing out ‘st’ elements after sorting “;
for( auto elem:st )
{
cout<< elem ;
}

cin.get( );
return 0;
}

Output

Printing out ‘st’ elements after sorting
=default
=delete
delete
new
using

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++ cstdlib bsearch