Question: Hello how can help me with this problem . For each of the following algorithms, determine the complexity in Big O notation in as simplified

Hello how can help me with this problem .

For each of the following algorithms, determine the complexity in Big O notation in as simplified a form as possible. Explain your solutions.

  1. Naive Fibonacci calculation. State the complexity in terms of n.

    public static int simpleFib(int n) { if (n == 0 || n == 1) { return 1; } else { return simpleFib(n - 2) + simpleFib(n - 1); } }
  2. Improved Fibonacci calculation. State the complexity in terms of n.

    public static int goodFib(int n) { int[] fibs = new int[n + 1]; fibs[0] = 1; fibs[1] = 1; for (int i = 2; i < fibs.length; i++) { fibs[i] = fibs[i - 2] + fibs[i - 1]; } return fibs[n]; }
  3. Simple sorting. State the complexity in terms of the length of arr.

    public static void sort(int[] arr) { for (int i = arr.length - 2; i >= 0; i--) { for (int j = 0; j <= i; j++) { if (arr[j] > arr[j + 1]) { int tmp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = tmp; } } } }
  4. The indexOf(char[] needle, char[] hay) method in the sample solution of assignment 9.3. State the complexity in terms of the lenths of needle and hay.

  5. Determining the index of a given number in a sorted array of numbers. State the complexity in terms of the length of hay.

    public static int sortedIndexOf(int needle, int[] hay) { int lowerBound = 0; int upperBound = hay.length - 1; while (lowerBound <= upperBound) { int mid = (lowerBound + upperBound) / 2; if (hay[mid] < needle) { lowerBound = mid + 1; } else if (hay[mid] > needle) { upperBound = mid - 1; } else { return mid; } } return -1; }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!