find_minimum/find_minimum.c
int find_minimum (const unsigned int *a, size_t s, unsigned int *n_iter)
{
        size_t from = 0;
        size_t to = s-1;

        if (n_iter)
                *n_iter = 0;

        if (s == 0)
                return -1;

        while (1) {
                size_t m = (from + to) / 2;

                if (n_iter)
                        (*n_iter)++;

                if (to == from) {
                        return a[from];
                } else if ((to - from) == 1) {
                        return ((a[to] < a[from]) ? a[to] : a[from]);
                } else if (a[m] < a[m-1] && a[m] < a[m+1]) {
                        /* current element is local minimum */
                        return a[m];
                } else if (a[m-1] < a[m]) {
                         /* shrink to left partition" */
                        to = m-1;
                } else {
                        /* shrink to right partition otherwise */
                        from = m+1;
                }
        }
}
/**
 * find_minimum: find local minimum in given array (binary search)
 *
 * XXX: elements are guaranteed to be distinct (or can't be done in O(log n))
 *
 */
find local minimum in given array (binary search)
XXX: elements are guaranteed to be distinct (or can't be done in O(log n))
find local minimum in given array ( binary search ) XXX : elements are guaranteed to be distinct ( or can ' t be done in O ( log n ))
int find_minimum (const unsigned int *a, size_t s, unsigned int *n_iter)
{
        size_t from = 0;
        size_t to = s-1;
        if (n_iter)
                *n_iter = 0;
        if (s == 0)
                return -1;
        while (1) {
                size_t m = (from + to) / 2;
                if (n_iter)
                        (*n_iter)++;
                if (to == from) {
                        return a[from];
                } else if ((to - from) == 1) {
                        return ((a[to] < a[from]) ? a[to] : a[from]);
                } else if (a[m] < a[m-1] && a[m] < a[m+1]) {
                        return a[m];
                } else if (a[m-1] < a[m]) {
                        to = m-1;
                } else {
                        from = m+1;
                }
        }
}
int find_minimum ( const unsigned int * a , size_t s , unsigned int * n_iter ) { size_t from = 0 ; size_t to = s - 1 ; if ( n_iter ) * n_iter = 0 ; if ( s == 0 ) return -1 ; while ( 1 ) { size_t m = ( from + to ) / 2 ; if ( n_iter ) ( * n_iter ) ++ ; if ( to == from ) { return a [ from ] ; } else if ( ( to - from ) == 1 ) { return ( ( a [ to ] < a [ from ] ) ? a [ to ] : a [ from ] ) ; } else if ( a [ m ] < a [ m - 1 ] && a [ m ] < a [ m + 1 ] ) { return a [ m ] ; } else if ( a [ m - 1 ] < a [ m ] ) { to = m - 1 ; } else { from = m + 1 ; } } }
find_minimum: find local minimum in given array (binary search)
 XXX: elements are guaranteed to be distinct (or can't be done in O(log n))
find_minimum : find local minimum in given array ( binary search ) XXX : elements are guaranteed to be distinct ( or can ' t be done in O ( log n ))
/* current element is local minimum */ /* shrink to left partition" */ /* shrink to right partition otherwise */