Advertisement
Guest User

Untitled

a guest
Jul 18th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.21 KB | None | 0 0
  1. #include<stdio.h>
  2.  
  3. int main(){
  4.  
  5. int a[10] = {2,57,3,45,46,1,7,9,21,17};
  6. int i, j, min, aux;
  7.  
  8. for(i=0;i<10;i++){
  9. min = i;
  10. for(j=i+1;j<10;j++){
  11. if(a[j] < a[min]){
  12. min = j;
  13. }
  14. }
  15. aux = a[i];
  16. a[i] = a[min];
  17. a[min] = aux;
  18. }
  19.  
  20. printf("n");
  21. for(i=0;i<10;i++){
  22. printf(" %i ", a[i]);
  23. }
  24. printf("n");
  25.  
  26.  
  27. //Busqueda binaria
  28.  
  29. int dato, inf, sup, mitad;
  30. char band = 'F';
  31.  
  32. printf("ntDigite un numero a buscar: ");
  33. scanf("%i", &dato);
  34.  
  35. inf = 0; //primera posicion
  36. sup = 5; //numero maximo de elementos
  37.  
  38. while(inf<=sup){
  39. mitad = (inf + sup)/2;
  40.  
  41. if(a[mitad] == dato){
  42. band = 'T';
  43. break; //Para finalizar el bucle while
  44. }
  45. if(a[mitad] > dato){
  46. sup = mitad;
  47. mitad = (inf + sup)/2;
  48. }
  49. if(a[mitad] < dato){
  50. inf = mitad;
  51. mitad = (inf + sup)/2;
  52. }
  53. }
  54.  
  55. if(band == 'F'){
  56. printf("nt The number does not exists");
  57. }
  58.  
  59. else if(band == 'T'){
  60. printf("ntThe number exists in position %i", mitad);
  61. }
  62.  
  63. return 0;
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement