Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /** p21 cautare binara*/
- #include<stdio.h>
- #include<stdlib.h>
- #include<string.h>
- #include<iostream>
- using namespace std;
- int BinarySearch2(string cheie, string a[], int prim, int ultim) {
- if(prim>ultim) {
- return -1;
- }
- int x=(ultim-prim)/3;
- int mij1=prim+x;
- int mij2=prim+2*x/3;
- if(cheie==a[mij1]) {
- return mij1;
- } else {
- if(cheie==a[mij2]) {
- return mij2;
- }
- }
- if(cheie<a[mij1])
- return BinarySearch2(cheie, a,prim, mij1-1);
- if(a[mij1]<cheie && cheie<a[mij2]) {
- return BinarySearch2(cheie, a,mij1+1, mij2-1);
- } else {
- return BinarySearch2(cheie, a, mij2+1, ultim);
- }
- }
- int FindStringInSortedArray(string cheie, string a[], int n) {///functie wrapper
- return BinarySearch2(cheie,a,0,n-1);
- }
- int main() {
- int n=6;
- string cheie;
- string a[6]= {"a","b","c","d","e","f"};
- cout<<"Dati cheia"<<endl;
- cin>>cheie;
- int index=FindStringInSortedArray(cheie,a,6);
- cout<<" Cheia se gaseste pe pozitia: "<<index;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement