Question

Need some help with finding the runtime of these java functions in the form of exact,...

Need some help with finding the runtime of these java functions in the form of exact, tilde and big oh notation:

Example of how:

Func Exact Tilde O()
Example 1 1 ~1 O(1)
Example 2 2n+1 ~2n O(2n)

Here are the functions:

void funcA(int n) {

        step();

        for (int i = 0; i < n; i++) {

            funcA(n-1);

        }

    }

    void funcB(int n) {

        step();

        if (n > 0)

            funcB(n-1);

    }

    void funcC(int n) {

        step();

        if (n > 0) {

            funcC(n/2);

        }

    }

  

void funcD(int n) {

        if (n > 0) {

            funcD(n/2);

        }

        for (int i = 0; i < n; i++) {

            step();

        }

        if (n > 0) {

            funcD(n/2);

        }

    }

void funcE(int n) {

        Random random = new Random();

        int target = random.nextInt(n);

        int lo = 0;

        int hi = n;

        while (hi - lo > 1) {

            step();

            int mid = (lo + hi) / 2;

            if (mid > target) {

                hi = mid;

            }

            else {

                lo = mid;

            }

        }

    }

   

void funcF(int n) {

        step();

        if (n > 0) {

            funcF(n-1);

            funcF(n-1);

            funcF(n-1);

        }

    }

   

void funcG(int n) {

        for (int i = 0; i < n; i++) {

            step();

            for (int j = 1; j <= n; j *= 3) {

                step();

            }

        }

    }

   

void funcH(int n) {

        step();

        for (int i = 1; i <= n; i *= 2) {

            step();

            step();

        }

        for (int i = 0; i <n; i++) {

            step();

        }

    }

   

void funcI(int n) {

        int i = n * n * n;

        while (i > 0) {

            step();

            for (int j = 0; j * j < n; j++) {

                step();

                i--;

            }

        }

0 0
Add a comment Improve this question Transcribed image text
Answer #1

void funcA(int n) {

        step();

        for (int i = 0; i < n; i++) {

            funcA(n-1);

        }

    }

Exact: n*(n-1)

Tilde: ~n2

O(): O(n2)

    void funcB(int n) {

        step();

        if (n > 0)

            funcB(n-1);

    }

Exact: n-1

Tilde: ~n

O(): O(n)

    void funcC(int n) {

        step();

        if (n > 0) {

            funcC(n/2);

        }

    }

Exact: log2n

Tilde: ~log2n

O(): O(log2n)

  

void funcD(int n) {

        if (n > 0) {

            funcD(n/2);

        }

        for (int i = 0; i < n; i++) {

            step();

        }

        if (n > 0) {

            funcD(n/2);

        }

    }

The recurrence relation is given as: T(n) = 2T(n/2) + O(n)

Exact: n*log2n

Tilde: ~n*log2n

O(): O(n*log2n)

NOTE: As per HOMEWORKLIB POLICY I am allowed to answer 4 questions (including sub-parts) on a single post. Kindly post the remaining questions separately and I will try to answer them. Sorry for the inconvenience caused.

Add a comment
Know the answer?
Add Answer to:
Need some help with finding the runtime of these java functions in the form of exact,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • PROBLEM 1 (24 points): For each of the recursive functions below and on the next page,...

    PROBLEM 1 (24 points): For each of the recursive functions below and on the next page, give a correct recurrence relation expressing its runtime and then a tight runtime bound for the recurrence relation. Functions that take parameters lo and hi: n=hi-lo+1 RECURRENCE RELATION: int foo(int a[], int lo, int hi) { int x, i, j, m; X = 0; if(lo==hi) return a[10]; for(i=lo; i<=hi; i++) { for(j=i+1; j<=hi; j++) { if(a[i] % 2) x += a[i]; else x -=...

  • Analyze the runtime of c functions below and give a tight runtime bound for each. ....

    Analyze the runtime of c functions below and give a tight runtime bound for each. . . Both functions have the same best-case and worst-case runtime (so this is not an issue). Since we want a "tight" runtime bound, your final answer should be in big-m form. Show your work! "The runtime of foo() is e(< something >)" is not sufficient even if <something> happens to be correct. In other words, convince the reader of the correctness of your answer....

  • 3. Recursive Program (6 points) Consider the following recursive function for n 1: Algorithm 1 int...

    3. Recursive Program (6 points) Consider the following recursive function for n 1: Algorithm 1 int recurseFunc(int n) If n 0, return 1. If n 1, return 1 while i< n do while j <n do print("hi") j 1 end while i i 1 end while int a recurse Func(n/9); int b recurse Func (n/9) int c recurse Func (n/9) return a b c (1) Set up a runtime recurrence for the runtime T n) of this algorithm. (2) Solve...

  • USE JAVA PROGRAMMING Create a program that would collect list of persons using double link list...

    USE JAVA PROGRAMMING Create a program that would collect list of persons using double link list and use a Merge Sort to sort the object by age. Create a class called Person : name and age Create methods that add, and delete Person from the link list Create a method that sorts the persons' objects by age. package mergesort; public class MergeSortExample { private static Comparable[] aux; // auxiliary array for merges public static void sort(Comparable[] a) { aux =...

  • What is the runtime of each method? Give answer in Θ(big Theta) notation as a function...

    What is the runtime of each method? Give answer in Θ(big Theta) notation as a function of n, give a brief explanation. A. public static int method1(int n){ int mid = n/2; for (int i = mid; i >= 0; i--) System.out.println(i); for (int i = mid + 1; i <= n; i++) System.out.println(i); return mid; } B. public static int method2(int n){ for (int i = n; i >= 0; i / 3){ System.out.println(i ); } return mid; }...

  • This is an assignment for my algorithm class which I have written the code partially and...

    This is an assignment for my algorithm class which I have written the code partially and only need to complete it by adding a time function that calculates the average running time. We could use any programming language we want so I am using C++. I am including the instruction and my partial code below. Thank you! Implement linearSearch(a,key) and binarySearch( a,key)functions. Part A.In this part we will calculate theaverage-case running time of each function.1.Request the user to enter a...

  • They NAME sc 162- lec. 18 (Big quiz 1. Arrange the following functions in order of...

    They NAME sc 162- lec. 18 (Big quiz 1. Arrange the following functions in order of increasing rate of growth. Also, identify any functions with the SAME rate of growth by putting then below the others. a) sn, 44log n, 10n log n, 500, 2n, 28, 3n b) n', n +2 nlog2 n, n! ne log, n, n n n'. 4", n, na, 2 2. Use the Big-o notation to estimate the time complexity for the following segments/methods. (Assume all...

  • Java Im doing a binary search on an array and need to allow as many queries...

    Java Im doing a binary search on an array and need to allow as many queries as possible and cannot figure out how to. The output should read something like this. Enter a number. 3 3 is a prime number. Enter another number. 4 4 is not a prime number. Enter another number. 8 Current file not large enough for 8. Enter another number. -1 Bye. My code so far looks like this. public static void main(String[] args)    {...

  • 1. t(n) is the runtime of following function, public static int f4(int [] a, int start,...

    1. t(n) is the runtime of following function, public static int f4(int [] a, int start, int end){ int ans = 0; if (start >= end) ans = a[start]; else { int mid = (start + end) / 2; int x = f4(a, start, mid); int y = f4(a, mid + 1, end); print(a, start, end); //print each element in a from start to end if (x < y) ans = x; else ans = y; } return ans; }...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT