Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- #define MAX 100
- void bubblesort(int vett[], int dim);
- int ricercaBinaria(int vett[], int dim, int x);
- main ()
- {
- int vettore[MAX];
- int i, dim, x, m = -1;
- printf("posti dell'array : ");
- scanf("%d", &dim);
- for (i = 0; i < dim; i++) //INSERIMENTO VALORI ARRAY
- {
- scanf("%d", &vettore[i]);
- }
- bubblesort(vettore, dim); //BUBBLESORT
- for (i = 0; i < dim; i++) //STAMPA ARRAY ORDINATO
- {
- printf("%d ",vettore[i]);
- }
- printf("\nInserisci valore da ricercare: "); //VALORE DA RICERCARE
- scanf("%d", &x);
- m = ricercaBinaria(vettore, dim, x);
- if (m == -1) printf("Il numero non e presente nello arai mamaormabilio");
- else printf("Il numero inserito e presente in psoizione %d", m);
- }
- void bubblesort(int vett[], int dim)
- {
- int i, tmp, flag;
- do //BUBBLESORT ORDINAMENTO
- {
- flag = 0;
- for (i = 0; i < dim-1; i++)
- {
- if (vett[i] > vett[i+1])
- {
- tmp = vett[i];
- vett[i] = vett[i+1];
- vett[i+1] = tmp;
- flag = 1;
- }
- }
- } while (flag == 1);
- printf("\n");
- }
- int ricercaBinaria(int vett[], int dim, int x)
- {
- int p, u, m; //ALGORITMO RICERCA BINARIA
- p = 0; //p INIZIO u FINE m MEZZO ARRAY
- u = dim-1;
- while (p <= u)
- {
- m = (p + u) / 2;
- if (vett[m] == x)
- return m;
- else if (vett[m] < x)
- p = m + 1;
- else u = m - 1;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement