Advertisement
Hydrase

Binary Search

Aug 6th, 2024
27
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.54 KB | None | 0 0
  1. #include<iostream.h>
  2. #include<conio.h>
  3. int main()
  4. {
  5. int a[10],i,n,mid,beg,end,item;
  6. clrscr();
  7. cout<<"\nEnter the value of n:";
  8. cin>>n;
  9. cout<<"Enter elements";
  10. for(i=0;i<n;i++)
  11. cin>>a[i];
  12. cout<<"\nEnter the item to be searched:";
  13. cin>>item;
  14. beg=0;
  15. end=n-1;
  16. mid=(int)(beg+end)/2;
  17. while(beg<=end&&a[mid]!=item)
  18. {
  19. if(item<a[mid])
  20. end=mid-1;
  21. else
  22. beg=mid+1;
  23. mid=(int)(beg+end)/2;
  24. }
  25. if(a[mid]==item)
  26. cout<<"\nSearch is successful";
  27. else
  28. cout<<"\nSearch is unsuccessful";
  29. getch();
  30. return 0;
  31. }
  32.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement