Advertisement
apl-mhd

BinarySearchWithRecursion

Feb 13th, 2017
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.86 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3.  
  4. using namespace std;
  5.  
  6.  
  7. int bSearch(int number[],int low, int high, int mid,int item){
  8.    
  9.        
  10.         if(low > high)
  11.             return 0;
  12.        
  13.         if(number[mid] == item)
  14.             return 1;
  15.        
  16.         else if(number[mid] >item){
  17.            
  18.                 high = mid-1;
  19.             }
  20.            
  21.             else
  22.            
  23.                 low = mid+1;
  24.                
  25.        
  26.     bSearch(number, low, high, (low+high)/2, item);
  27.        
  28.         //return 0;
  29.        
  30.    
  31.     /*
  32.     int low,high,mid;
  33.    
  34.     low = 0;
  35.     high = n-1;
  36.    
  37.     while(low<=high){
  38.        
  39.         mid = (low+high) / 2;
  40.        
  41.         if(number[mid] == item)
  42.            
  43.             return 1;
  44.            
  45.         else if(number[mid] > item)
  46.            
  47.                 high = mid-1;
  48.        
  49.             else
  50.                
  51.                 low = mid+1;
  52.        
  53.        
  54.         }
  55.        
  56.     return 0;
  57.    
  58.     */
  59.    
  60.     }
  61.  
  62. int main(int argc, char **argv)
  63. {
  64.    
  65.     int number[10] = {1,2,3,4,5,6,7,8,9,10};
  66.    
  67.    
  68.     int n=10;
  69.    
  70. printf(" %d ", bSearch(number, 0, n-1, (n-1)/2, -1));
  71.    
  72.    
  73.     return 0;
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement