fork download
  1. #include <stdio.h>
  2. #include <string.h>
  3. int n,x;
  4. void linear(int a[]){
  5. int found=0;
  6. for(int i=0;i<n;i++){
  7. if(x==a[i]){
  8. found=1;
  9. }}
  10. if (found==1){
  11. printf("\nfound\n");
  12. }else printf("\nnot found");
  13. }
  14. void binary(int a[]){
  15. int low,mid,high,found;
  16. found=0;
  17. low=0;
  18. high=n-1;
  19. while(low<high){
  20. mid = (low+high)/2;
  21. if(x<a[mid]){
  22. high=mid-1;
  23. }else if(x>a[mid]){
  24. low=mid+1;
  25. }else{
  26. found=1;
  27. break;
  28. }}
  29. if (found==1){
  30. printf("\nFound");
  31. }else printf("\nnot found");
  32. }
  33. int main() {
  34. printf("Enter the number of elements:\n");
  35. scanf("%d",&n);
  36. int a[n];
  37. printf("Enter the elements:\n");
  38. for(int i=0;i<n;i++){
  39. scanf("%d",&a[i]);
  40. }
  41. printf("Enter the number to be found:\n");
  42. scanf("%d",&x);
  43. linear(a);
  44. binary(a);
  45. return 0;
  46. }
Success #stdin #stdout 0s 5320KB
stdin
Standard input is empty
stdout
Enter the number of elements:
Enter the elements:
Enter the number to be found:

not found
not found