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:

Picture 1 of The qsort () function in C

According to Tutorialspoint

Previous lesson: Function bsearch () in C

Next lesson: Function abs () in C

Update 25 May 2019
Category

System

Mac OS X

Hardware

Game

Tech info

Technology

Science

Life

Application

Electric

Program

Mobile