Link to home
Start Free TrialLog in
Avatar of desperado
desperado

asked on

Complexity in algorithms

I got the following algorithm and I am trying to figure out the complexity of it if i call max(a,1,n):

max(a,i,n)
if i<n then x=max(a,i+1,n)
       else return(a[n])
if x<a[i] then return(x)
          else return(a[i])
Avatar of nietod
nietod

We cannot provide answers to school assignments.  That is grounds for removal from this site.  (for both you and the experts involved.)  We can provide only limitied help in accademic assignments.    We can answer specific (direct) questions, like you might ask your teacher.  We can review your work and post suggestions, again, like your teacher might do.

Do you have specific questions?
Do you have any work on this (incomplete even) that we can review?



I can tell you this much, the duration of the algorithm is proportional to the number of times it calls itself recusrively.  So you need to figure out how many times the function calls itself.

i.e you will have a formula based on a, i, and n (it is possible that 1 or 2 of these might not appear in the formula) that tells you how many times max() will be called.
Avatar of desperado

ASKER

I read the license agreement here. I posted the question like it is because it is not an assigment. I'm learning complexity by myself.
The work i have is the algorithm that is posted in the question. I aslo made this program to test if the algorithm worked. It does get the maximum:

#include<iostream.h>
int max(int [],int,int);
main()
{
 int a[5]={200,10,100,6,8};
 int maxi;
 maxi=max(a,0,4);
 cout<<"The maximum is:"<<maxi;
 return(0);
}

/*function founds the maximum number and returns it.Funtion goes through all the elements in the array and then compares them to find maximum.*/

max(int a[],int i,int n)
{
 int x;
 if(i<=n)
 { x=max(a,i+1,n);
 }
 else
 {
      return(a[n]);
 }
 if(x>a[i])
 {
      return(x);
 }
 else
      return(a[i]);
}

its theta of n.
t(n)=t(n-1) + theta(1)
by re-insertions u can see its the theta of n.

adi.
ASKER CERTIFIED SOLUTION
Avatar of nietod
nietod

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial