Help In Complexity

Revision en1, by nikizakr, 2018-01-15 22:54:55

the build function in segment tree :

void build(int s = 1 , int e = n , int p = 1){ 
    if(s == e) {
      seg[p] = 1 ;
      return ; 
    }
    build(s , (s + e)/2 , 2 * p) ;
    build((s + e)/2 + 1 , e , 2 * p + 1);
}

divide and conquer algorithm to find maximum element in array :

int Max(int s = 1 , int e = n){
      if(s == e) return arr[s] ; 
      int choice1 = arr[s] , choice2 = arr[e] ; 
      choice1 = max( choice1 , Max( s , (s + e)/2 ) ; 
      choice2 = max( choice2 , Max( (s + e)/2 + 1 , e )   ;
     return max(choice1 , choice2)  ; 
}

why the complexity of the first function is O( n * log(n) ) And the second is O(n) thanks for your help :D

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en2 English nikizakr 2018-01-15 23:02:49 171
en1 English nikizakr 2018-01-15 22:54:55 742 Initial revision (published)