The qsort () function in C
Void qsort function (void * base, so-phan-tu, kich-co, int (* compar) (const void *, const void *)) arranges an array.
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've just finished reading the article "The qsort () function in C" edited by the TipsMake team. You can save the-qsort-function-in-c.pdf to your computer here to read later or print it out. We hope this article has provided you with many useful tech tips and tricks. You can search for similar articles on tips and guides. Thank you for reading and for following us regularly.