summaryrefslogblamecommitdiffstats
path: root/common/sys/sort.c
blob: 78b0af2de86ab672d70d5033c704afefe5dc31e6 (plain) (tree)
1
2
3
4
5
6
7
8
9
                  







                                                

                               
 









































                                                                               
#include <stdio.h>

int cmp_int(const void *a, const void *b)
{
    return *(int*)a - *(int*)b;
}

int cmp_int_desc(const void * a, const void * b)
{
    // return cmp_int(b, a);
    return *(int*)b - *(int*)a;
}

int *
intbsearch(int key, const int *base0, int nmemb)
{
    /* 寡 /usr/src/lib/libc/stdlib/bsearch.c ,
       撠蝯行 int array 函, 銝€ compar function 頛敹思 */
    const   char *base = (const char *)base0;
    size_t  lim;
    int     *p;

    for (lim = nmemb; lim != 0; lim >>= 1) {
    p = (int *)(base + (lim >> 1) * 4);
    if( key == *p )
        return p;
    if( key > *p ){/* key > p: move right */
        base = (char *)p + 4;
        lim--;
    }               /* else move left */
    }
    return (NULL);
}

unsigned int *
uintbsearch(const unsigned int key, const unsigned int *base0, const int nmemb)
{
    /* 寡 /usr/src/lib/libc/stdlib/bsearch.c ,
       撠蝯行 int array 函, 銝€ compar function 頛敹思 */
    const   char *base = (const char *)base0;
    size_t  lim;
    unsigned int     *p;

    for (lim = nmemb; lim != 0; lim >>= 1) {
        p = (unsigned int *)(base + (lim >> 1) * 4);
        if( key == *p )
            return p;
        if( key > *p ){/* key > p: move right */
            base = (char *)p + 4;
            lim--;
        }               /* else move left */
    }
    return (NULL);
}