Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //binary search
- #define _CRT_SECURE_NO_WARNINGS
- #include <stdio.h>
- int main() {
- int n, v[100], st, dr,aux ,j, mij, x, gasit, i;
- printf("Introduceti numarul de elemente: ");
- scanf("%d", &n);
- printf("Introduceti %d numere: \n", n);
- for (i = 0; i < n; i++)
- scanf("%d", &v[i]);
- for(i=0; i<n; i++)
- for(j=i; j<n; j++)
- if(v[i]< v[j])
- {
- aux = v[i];
- v[i] = v[j];
- v[j] = aux;
- }
- printf("Valoarea cautata in vector: ");
- scanf("%d", &x);
- gasit = 0;
- st = 0;
- dr = n - 1;
- while (st <= dr && gasit == 0)
- {
- mij = (st + dr) / 2;
- if (v[mij] == x)
- gasit = 1;
- else
- if (v[mij] > x)
- dr = mij - 1;
- else
- st = mij + 1;
- }
- if (gasit)
- printf("Numarul cautat exista in vector!");
- else
- printf("Numarul cautat nu exista in vector!");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement