Advertisement
mrzrashed

Final_Binary_Search

Dec 14th, 2016
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.60 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. int main()
  4. {
  5.     int a[10]={1,2,3,4,5,6,7,8,9,10};
  6.     int counter;
  7.     int item;
  8.     int beg=0,end=9;
  9.     int mid;
  10.     mid=(beg+end)/2;
  11.     printf("Please enter the element to search: ");
  12.     scanf("%d",&item);
  13.  
  14.     while(beg<=end && a[mid]!=item){
  15.         if(item>a[mid]){
  16.             beg=mid+1;
  17.         }
  18.         else{
  19.             end=mid-1;
  20.         }
  21.         mid=(beg+end)/2;
  22.     }
  23.     if(a[mid]==item){
  24.         printf("The item %d is found: ",item);
  25.     }
  26.     else{
  27.         printf("The item %d is not found: ",item);
  28.     }
  29.     return 0;
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement