Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

BinarySearch.c

By: a guest on Apr 9th, 2013  |  syntax: C  |  size: 0.67 KB  |  views: 38  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  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. }