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
Dining room design mistakes to avoid
6 mistakes to avoid when using Gmail at work
How to Avoid Post-Tracking Targeted Ads After Shopping Online
The Truth About Salt: How to Avoid One of the World's Most Dangerous Silent Killers
8 Mistakes to Avoid on WhatsApp
10 Important Mistakes to Avoid When Maintaining Your Computer