






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







T(0) = c T(n) = b + T(n - 1) = b + b + T(n - 2) = b +b +b + T(n - 3) … = kb + T(n - k) When k = n, we have: T(n) = nb + T(n - n) = bn + T(0) = bn + c. Therefore method factorial is O(n). long factorial (int n) { if (n == 0) return 1; else return n * factorial (n – 1); }
2
public int binarySearch (int target, int[] array, int low, int high) { if (low > high) return -1; else { int middle = (low + high)/2; if (array[middle] == target) return middle; else if(array[middle] < target) return binarySearch(target, array, middle + 1, high); else return binarySearch(target, array, low, middle - 1); } }
public static void hanoi(int n, char from, char to, char temp){ if (n == 1) System.out.println(from + " --------> " + to); else{ hanoi(n - 1, from, temp, to); System.out.println(from + " --------> " + to); hanoi(n - 1, temp, to, from); } }
Expanding: T(n) = 2T(n – 1) + b = 2[2T(n – 2) + b] + b = 22 T(n – 2) + 2b + b = 2^2 [2T(n – 3) + b] + 2b + b = 23 T(n – 3) + 2^2 b + 2b + b = 2^3 [2T(n – 4) + b] + 2^2 b + 2b + b = 24 T(n – 4) + 2^3 b + 2^2 b + 2^1 b + 2^0 b = …… = 2 k^ T(n – k) + b[2k- 1^ + 2k– 2^ +... 2^1 + 2^0 ] When k = n – 1, we have:
n