Advertisement
kabbo512

Untitled

Jul 12th, 2019
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.64 KB | None | 0 0
  1. //Binary search
  2. #include<stdio.h>
  3. int main()
  4. {
  5.     int arr[12] = {12,23,30,32,38,42,50,60,65,70,85,90,100};
  6.     int start,end,mid,key;
  7.     start=0;
  8.     end=12;
  9.     key = 32;
  10.  
  11.     do{
  12.         mid = (start+end)/2;
  13.         if(arr[mid] == key)
  14.         {
  15.             printf("Value is found at %dth position\n",mid+1);
  16.             break;
  17.         }
  18.         else if(arr[mid] > key)
  19.         {
  20.             end = mid-1;
  21.         }
  22.         else
  23.         {
  24.             start = mid+1;
  25.         }
  26.  
  27.  
  28.     }
  29.     while(start <= end);
  30.  
  31.         if(start>end)
  32.         {
  33.             printf("failed");
  34.         }
  35.  
  36.  
  37.  
  38.  
  39.     return 0;
  40.  
  41.  
  42.  
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement