Advertisement
Guest User

BinarySearch.c

a guest
Apr 9th, 2013
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.67 KB | None | 0 0
  1. #include <stdio.h>
  2. #include "Proto.h"
  3.  
  4. int binarySearch(char * nums, char svalue, int start, int end)
  5. {
  6.     middle = (start + end) / 2;
  7.    
  8.     /* Target found */
  9.     if (nums[middle] == svalue)
  10.     {
  11.         return middle;
  12.     }
  13.    
  14.     /* Target not in list */
  15.     else if( start == end )
  16.     {
  17.         return -1;
  18.     }
  19.    
  20.     /* Search to the left */
  21.     else if( nums[middle] > svalue )
  22.     {
  23.         count++;
  24.         return binarySearch( nums, svalue, start, (middle-1) );
  25.     }
  26.    
  27.     /* Search to the right */
  28.     else if( nums[middle] < svalue )
  29.     {
  30.         count++;
  31.         return binarySearch( nums, svalue, (middle+1), end );
  32.     }
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement