tags:

views:

225

answers:

2
int ara(int dizi[], int ilk, int son, int deger) { 
      int indeks;    
      if ( ilk > son ) 
        return 0; 

      indeks = (ilk + son) / 2; 
      if ( dizi[indeks] < deger ) 
        return ara(dizi, indeks+1, son, deger); 
       else if ( dizi[indeks] > deger ) 
        return ara(dizi, ilk, indeks-1, deger); 
       else  
       return 1; 
} 

for (i=1; i<2*n; i++)   { 
    printf("Bir sayi giriniz: "); 
    scanf("%d", &sayi); 
    sonuc = ara(matrix, 0, n-1, sayi); 
    if ( sonuc == 1 ) 
      printf("Found!\n"); 
     else 
      printf("Not Found!\n");  
}

what can be the big-O notation of this code? my guess is N*(2^(logN))

I have assigned my hw already! this is just my pre-curiosity!

A: 

Looks linear to me. But your formatting is pretty screwed up, so it's hard to tell.

EDIT: Alright, recursion involved. Not linear, then. It would be easier if we had meaningful variable names.

Anon.
Just because you don't speak Turkish doesn't make the variable name non-meaningful :-)
paxdiablo
thanks paxdiablo! the vars are vars you know (x,y,z, bla. bla.).I am new here and I cannot insert int main() method into text area as code. so I omitted.
edib
+6  A: 

ara is a recursive implementation of binary search. That is O(log n).

It is called 2n-1 times. Multiplying the two terms, the program as a whole is O(n log n).

ephemient
You beat me to it by seconds!
David Thornley
thanks, I think your answer is correct.
edib