C language function qsort

School for so long do not know C language has this function (in fact, because basically never written C, except when school programming ..)

function prototype qsort is void __cdecl qsort (void * base,  size_t num, size_t width, int (__cdecl * comp) (const void *, const void *))
wherein the base is a collection of sorted array, num is the array element the number, width is the size of an element, comp is a comparison function. The default small to large.

Wherein invoking the algorithm is a fast sort, and is optimized three-way quick drain (divided than the reference value, the reference value is equal to, greater than the reference value).

Wherein the fourth parameter is a pointer to the function, the return value is int, if desired in ascending order: Parameter 1> 2 should return a positive number of parameters, parameter 1 <2 returns a negative parameter, parameter 2 Parameter 1 == 0 returns.

such as:

int cmp(const void* a,const void* b){
    return *(int*)(a)-*(int*)(b);
    }
int main(void)
{
    
    vector<int> p={3,432,52,12,5,3456,4523,43};
    qsort(&p[0],8,4,cmp);
    for(int x:p){
        cout<<x<<endl;
    }
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/FdWzy/p/12520991.html