Advertisement
diegoaguilar

Búsqueda binaria

Sep 22nd, 2011
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.66 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4.  
  5. int main ()
  6.  
  7. {
  8.  
  9. int A[15], x, i, s=0, f=15;
  10.  
  11. printf("\nIngrese el vector ordenado de menor a mayor\n");
  12. for (i=0; i<15; i++)
  13. {
  14. printf("Posición [%d]: ", i+1);
  15. scanf("%d", &A[i]);
  16. }
  17.  
  18. printf("\nEl vector ingresado es:\n");
  19. for (i=0; i<15; i++)
  20. {
  21. printf("\t[%d]", A[i]);
  22. }
  23.  
  24.  
  25. getchar();
  26. getchar();
  27. system("clear");
  28.  
  29.  
  30. printf("\n\t\t\t\tPropocione el número que desea buscar: ");
  31. scanf("%d", &x);
  32.  
  33. while (s<=f)
  34.  
  35. {
  36. i=s+(f-s)/2;
  37.  
  38. if (x>A[i])
  39. s=i+1;
  40.  
  41. else if (x<A[i])
  42. f=i-1;
  43.  
  44. else
  45. {
  46. printf("\n\nEl número %d se encuentra en el arreglo en la posición [%d]\n", x, i+1);
  47. break;
  48. }
  49.  
  50.  
  51. }
  52. return 0;
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement