Advertisement
Guest User

Untitled

a guest
Mar 24th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #define MAX 100
  4.  
  5. void bubblesort(int vett[], int dim);
  6. int ricercaBinaria(int vett[], int dim, int x);
  7. main ()
  8. {
  9.  
  10. int vettore[MAX];
  11. int i, dim, x, m = -1;
  12.  
  13. printf("posti dell'array : ");
  14. scanf("%d", &dim);
  15.  
  16. for (i = 0; i < dim; i++) //INSERIMENTO VALORI ARRAY
  17. {
  18. scanf("%d", &vettore[i]);
  19. }
  20.  
  21. bubblesort(vettore, dim); //BUBBLESORT
  22.  
  23. for (i = 0; i < dim; i++) //STAMPA ARRAY ORDINATO
  24. {
  25. printf("%d ",vettore[i]);
  26. }
  27.  
  28. printf("\nInserisci valore da ricercare: "); //VALORE DA RICERCARE
  29. scanf("%d", &x);
  30.  
  31. m = ricercaBinaria(vettore, dim, x);
  32.  
  33. if (m == -1) printf("Il numero non e presente nello arai mamaormabilio");
  34. else printf("Il numero inserito e presente in psoizione %d", m);
  35. }
  36.  
  37. void bubblesort(int vett[], int dim)
  38. {
  39. int i, tmp, flag;
  40. do //BUBBLESORT ORDINAMENTO
  41. {
  42. flag = 0;
  43. for (i = 0; i < dim-1; i++)
  44. {
  45. if (vett[i] > vett[i+1])
  46. {
  47. tmp = vett[i];
  48. vett[i] = vett[i+1];
  49. vett[i+1] = tmp;
  50. flag = 1;
  51. }
  52. }
  53. } while (flag == 1);
  54. printf("\n");
  55. }
  56.  
  57. int ricercaBinaria(int vett[], int dim, int x)
  58. {
  59. int p, u, m; //ALGORITMO RICERCA BINARIA
  60. p = 0; //p INIZIO u FINE m MEZZO ARRAY
  61. u = dim-1;
  62. while (p <= u)
  63. {
  64. m = (p + u) / 2;
  65. if (vett[m] == x)
  66. return m;
  67. else if (vett[m] < x)
  68. p = m + 1;
  69. else u = m - 1;
  70. }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement