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 should read it
Maybe you are interested
Unbelievable coincidence in history makes the whole world astounded How to directly see the phenomenon of super moon, blue moon and eclipse converging after more than 150 years The most anticipated astronomical events of 2017 How terrible is the rocket that will bring people to Mars? Learn about extraterrestrial super-Earth - Gliese 581c New discovery of life on planet GJ 1132b, 'Super Earth' is 39 light-years away