The qsort () function in C
The qsort () function in C
Void qsort function (void * base, so-phan-tu, kich-co, int (* compar) (const void *, const void *)) arranges an array.
Qsort () function stands for Quick Sort, written based on Quick Sort algorithm.
Declaring the function qsort () in C
Below is the declaration for qsort () in C:
void qsort ( void * base , so - phan - tu , kich - co , int (* compar )( const void *, const void *))
Parameters
base : This is the pointer to the first element of the array to be sorted.
so-phan-tu : This is the number of elements in the array pointed by the base pointer.
kich-co : This is the size (in bytes) of each element in the array.
compar : The function compares two elements.
Returns the value
This function does not return any values.
For example
The following C program illustrates the usage of qsort () in C:
#include #include int values [] = { 88 , 56 , 100 , 2 , 25 }; int cmpfunc ( const void * a , const void * b ) { return ( *( int *) a - *( int *) b ); } int main () { int n ; printf ( "Truoc khi sap xep, list co dang: n" ); for ( n = 0 ; n < 5 ; n ++ ) { printf ( "%d " , values [ n ]); } qsort ( values , 5 , sizeof ( int ), cmpfunc ); printf ( "nSau khi sap xep, list co dang: n" ); for ( n = 0 ; n < 5 ; n ++ ) { printf ( "%d " , values [ n ]); } return ( 0 ); }
Compiling and running the above C program will result:
According to Tutorialspoint
Previous lesson: Function bsearch () in C
Next lesson: Function abs () in C
You should read it
Maybe you are interested
4 Mistakes to Avoid When Setting Up a Password Manager
How to avoid grainy photos on smartphones
11 common mistakes when building a PC and how to avoid them
Build DTCL Void squad season 9
7 mistakes on social networks to avoid if you want to protect your privacy
4 Android features to help avoid distractions at work