1

In Big-Θ notation, analyze the running time of the following three pieces of code/pseudo-code, describing it as a function of the input, n.

1.

void f1(int n)
{
  int t = sqrt(n);
  for(int i = 0; i < n; i++){
    for(int j = 0; j < n; j++){
      // do something O(1)
      
    }
    n -= t;
  }
}
2. 
Assume A is an array of size n+1.

void f2(int* A, int n)
{
  for(int i=1; i <= n; i++){
    for(int k=1; k <= n; k++){
      if( A[k] == i){
        for(int m=1; m <= n; m=m+m){
          // do something that takes O(1) time
          // Assume the contents of the A[] array are not changed
        }
      }
    }
  }
}
3. 
void f3(int* A, int n)
{
  if(n <= 1) return;
  else {
    f3(A, n-2);
    // do something that takes O(1) time
    f3(A, n-2);
  }
}

Any help would be appreciated.

  • Stack Overflow really doesn't like doing people's homework for them. You need to at least put a little bit of effort into it. What do you think their time is, and why? – Frank Yellin Jan 26 '22 at 06:23
  • My bad, I didn't know how to include any of my work since I've been doing it on paper. – coding_phish Jan 26 '22 at 19:59

0 Answers0