Bisection finds a specified sequence of numbers

From , 5 Years ago, written in C, viewed 80 times.
URL https://pastebin.vip/view/806fec5a
  1. #include<stdio.h>
  2. int main(void)
  3. {
  4.     int arr[]={0,1,2,3,14,15,16,27,28,29},left,right,key,flag,mid;
  5.  
  6.     printf("Please input the number you want to search:\t");
  7.     scanf("%d",&key);
  8.     left=0;
  9.     right=9;
  10.     while(left<=right)
  11.     {
  12.         mid=(left+right)/2;
  13.         if(arr[mid]==key)
  14.         {
  15.             flag=1;
  16.             break;
  17.         }
  18.         else
  19.             if(key>arr[mid])
  20.             {
  21.                 left=mid+1;
  22.             }
  23.             else
  24.             {
  25.                 right=mid-1;
  26.             }
  27.     }
  28.     if(flag==1)
  29.     {
  30.         printf("The number %d is arr[%d] in the array.",key,mid);
  31.     }
  32.     else
  33.     {
  34.         printf("There is no %d in the array.\n",key);
  35.     }
  36.  
  37.     return 0;
  38. }
  39.  

Reply to "Bisection finds a specified sequence of numbers"

Here you can reply to the paste above

captcha

https://burned.cc - Burn After Reading Website