Binary search to find the range in which the number lies

后端 未结 10 730
悲哀的现实
悲哀的现实 2020-12-16 18:31

I have an array

Values array: 12 20 32 40 52
              ^  ^  ^  ^  ^
              0  1  2  3  4

on which I have to perform binary sear

相关标签:
10条回答
  • 2020-12-16 19:08
    /* binary_range.c (c) 2016 adolfo@di-mare.com  */
    /* http://stackoverflow.com/questions/10935635 */
    
    /* This code is written to be easily translated to Fortran */
    
    #include <stdio.h>   /* printf() */
    #include <assert.h>  /* assert() */
    
    /** Find the biggest index 'i' such that '*nSEED <= nVEC[i]'.
        - nVEC[0..N-1] is an strict ascending order array.
        - Returns and index in [0..N].
        - Returns 'N' when '*nSEED>nVEC[N-1]'.
        - Uses binary search to find the range for '*nSEED'.
    */
    int binary_range( int *nSEED, int nVEC[] , int N ) {
        int lo,hi, mid,plus;
    
        if ( *nSEED > nVEC[N-1] ) {
            return N;
        }
        for (;;) { /* lo = binary_range_search() */
            lo = 0;
            hi = N-1;
            for (;;) {
                plus = (hi-lo)>>1; /* mid = (hi+lo)/2; */
                if ( plus == 0 ) {   assert( hi-lo==1 );
                    if (*nSEED <= nVEC[lo]) {
                        hi = lo;
                    }
                    else {
                        lo = hi;
                    }
                }
                mid = lo + plus; /* mid = lo + (hi-lo)/2; */
    
                if (*nSEED <= nVEC[mid]) {
                    hi = mid;
                }
                else {
                    lo = mid;
                }
                if (lo>=hi) { break; }
            }
            break;
        } /* 'lo' is the index */
        /* This implementation does not use division. */
        /* =========================================  */
    
        assert( *nSEED <= nVEC[lo] );
        return lo;
    }
    
    /** Find the biggest index 'i' such that '*nSEED <= nVEC[i]'.
        - nVEC[0..N-1] is an strict ascending order array.
        - Returns and index in [0..N].
        - Returns 'N' when '*nSEED>nVEC[N-1]'.
        - Uses sequential search to find the range for '*nSEED'.
    */
    int sequential_range( int* nSEED, int nVEC[] , int N ) {
        int i;
        if ( *nSEED > nVEC[N-1] ) {
            return N;
        }
        i=0;
        while ( i<N ) {
            if ( *nSEED <= nVEC[i] ) { break; }
            ++i;
        }
        return i;
    }
    
    /** test->stackoverflow.10935635(). */
    void test_10935635() {
    {{  /* test.stackoverflow.10935635()                                  */
        /* http://stackoverflow.com/questions/10935635                    */
        /* binary_range search to find the range in which the number lies */
        /*              0  1  2  3  4                                     */
        int nVEC[] = { 12,20,32,40,52 }; int val;
        int N = sizeof(nVEC)/sizeof(nVEC[0]); /* N = DIM(nVEC[]) */
    
        val=19; val   = binary_range( &val,nVEC,N );
    
        /* 19 -> [12 < (19) <= 20] -> return 1 */
        val=19; assert( binary_range( &val,nVEC,N ) == 1 );
    
        /* 22 -> [20 < (22) <= 32] -> return 2 */
        val=22; assert( binary_range( &val,nVEC,N ) == 2 );
    
        /* 40 -> [32 < (40) <= 40] -> return 3 */
        val=40; assert( binary_range( &val,nVEC,N ) == 3 );
    
        /* Everything over 52 returns N */
        val=53; assert( binary_range( &val,nVEC,N ) == N );
    }}
    }
    
    /** Test program. */
    int main() {
        if (1) {
            printf( "\ntest_10935635()" );
            test_10935635();
        }
        printf( "\nEND" );
        return 0;
    }
    
    /* Compiler: gcc.exe (tdm-1) 4.9.2 */
    /* IDE:      Code::Blocks 16.01    */
    /* Language: C && C++              */
    
    /* EOF: binary_range.c */
    
    0 讨论(0)
  • 2020-12-16 19:10

    This will work under the condition that min(A[i]) <= key <=max(A[i])

    int binary_search(int A[],int key,int left, int right)
    {
    
      while (left <= right) {
            int middle = left + (right - left) / 2;
            if (A[middle] < key)
                left = middle+1;
            else if(A[middle] > key)
                right = middle-1;
            else
                return middle;
        }
        return (left - 1);
    }
    
    0 讨论(0)
  • 2020-12-16 19:14

    A regular binary search on success returns the index of the key. On failure to find the key it always stops at the index of the lowest key greater than the key we are searching. I guess following modified binary search algorithm will work.

    Given sorted array A
    Find a key using binary search and get an index. 
    If A[index] == key
        return index;
    else 
       while(index > 1 && A[index] == A[index -1]) index = index -1;
       return index;
    
    0 讨论(0)
  • 2020-12-16 19:14
    binsrch(array, num, low, high) {
    if (num > array[high])
         return high;
    
    
    while(1) {
         if (low == high-1)
              return low;
         if(low >= high)
              return low-1;        
         mid = (low+high)/2
         if (num < arr[mid])
              high = mid;
         else
              low = mid+1;
        }
    }
    
    0 讨论(0)
提交回复
热议问题