DragonOsman

Binary Search Attempt

Oct 23rd, 2016
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.14 KB | None | 0 0
  1. bool search(int value, int values[], int n)
  2. {
  3.     for (int i = 0; i < n; i++)
  4.     {
  5.         int start_point = values[0], end_point = values[n - 1];
  6.        
  7.         if (start_point > end_point)
  8.         {
  9.             // value doesn't exist in the array, so return false
  10.             // to mean we didn't find it
  11.             return false;
  12.         }
  13.        
  14.         int midpoint = 0;
  15.         if (n % 2 != 0)
  16.         {
  17.             midpoint = floor((start_point + midpoint) / 2);
  18.         }
  19.         else
  20.         {
  21.             int middle = floor((start_point + midpoint) / 2);
  22.             midpoint = floor(((middle + 2) + (middle - 2)) / 2);
  23.         }
  24.    
  25.         if (values[midpoint] == value)
  26.         {
  27.             return true;
  28.         }
  29.         else
  30.         {
  31.             if (value > values[midpoint])
  32.             {
  33.                 start_point = midpoint + 1;
  34.             }
  35.             else if (value < values[midpoint])
  36.             {
  37.                 end_point = midpoint - 1;
  38.             }
  39.             /*if (values[i] == value)
  40.             {
  41.                 return true;
  42.             }*/
  43.         }
  44.     }
  45.     return false;
  46. }
Add Comment
Please, Sign In to add comment