You are on page 1of 4

1.

Devise an algorithm that inputs three integers and outputs them in


non-decreasing order.

a[3] -> array having 3 elements


temp -> temporary variable
Input a[3]-------------------O(3)
for(i=0;i<3;i++){ ------------O(3)--------------------------------------------------|
for(j=0;j<3;j++){ ------ O(3)----------| |
if a[j+1]<a[j] ----------O(1) | |--O(3*12)
{ a[j] = temp ------O(1) |--O(3*(1+1+1+1))=O(3*4)=O(12) | =O(36)
a[j]=a[j+1] ------O(1) | |
a[j+1]=temp }----O(1)----------|-------------------------------- ------|
} }
Output a[3]----------------O(3)

Complexity: O(3+36+3)=O(42)

2. Present an algorithm that searches an unsorted array for the


element . If occurs, then return a position in the array; else return
zero.

a[n]-> array of n elements


x-> element to be searched
flag->count variable

i=0,flag=0; ---------------O(1)
while(i<n){ ----------------O(n)-----------------|
if(a[i]==x) --------------O(1)----| |
{ flag=i; --------------- O(1) | |-- O(n*(3))=O(3n)
terminate from loop}--O(1) |--O(3) |
else increment I---------O(1)----|-------------|
}
if(flag==0) ------O(1)---------|
return 0-----O(1) |
else |---O(2)
return i------O(1)---------|

Complexity: O(1+3n+2) = O(3n+3)

3. Factorial: Recursive
n-> a positive number whose factorial is to be found
fact(n){
if n<=1----------------------O(1) -----|
return 0 -----------O(1) |
else |--O(1+n)
return n*fact(n-1)---O(n) --|
Complexity: O(n+1)
4.Factorial: Iterative

n-> a positive number whose factorial is to be found


fact-> 1
if(n==0) --------------O(1)- -------------------------------------------|
fact=1 -------------O(1) |
else |---O(3n+1)
while(n!=0) ----------------O(n) -----------| |
{fact=fact*n -------------O(2) |---O(n(2+1))=O(3n) |
decrement n by 1} -------O(1) ------------|----------------------|

Complexity: O(3n+1)

5.Fibonnacci number - Recursive

n-> number of fibonnacci terms


ans-> required answer
fib(n){
if n<=1------------------------------O(1)-----------|
ans=1 ------------------------O(1) |
else |---O(2n-1 + 2n-2 +1)
n-1 n-2
ans=fib(n-1)+fib(n-2) ------O(2 + 2 )---|
}
Complexity: O(2n-1 + 2n-2 + 1)

6.Give an iterative algorithm to compute the binomial coefficient

n and k -> 2 no.s using which we have to find binomial coefficient as in nCk and n>k.
arr[n+1][k+1]->array required to store solution

while(i<=n){ ----------------------O(n)-----------------------------------|
for(j=0;j<( (i<k) ? i:k);j++)---------------O(k)----------| |
{if j=0 or j=i --------------------------O(1)---| |-O(k*3) = O(3k) |-O(n*3k)
c[i][j]=0-------------------------------O(1) |-O(3)| | =O(3nk)
else c[i][j]=c[i-1][j-1]+c[i-1][j]}}-------O(2)---|------|------------------------|

Complexity: O(3nk)
7.Give a recursive algorithm to compute the binomial coefficient

n and k -> 2 no.s using which we have to find binomial coefficient as in nCk.

bin(n,k){
if n==k or k==0------------------O(1)-----------|
return 1 ----------------O(1) |
else |-O(2n-1 + 1)
n-1
bin(n-1,k-1) + bin(n-1,k)-----O(2 )------|

Complexity : O(2n-1 + 1)

8. Ackermann’s function:

m and n -> 2 no.s required for evaluating Ackermann's function

Stack<Integer> s = new Stack<Integer>;------------O(1)


s.add(m); ------------O(1)
while (s not empty){ ------------------------O(n)-------------|
m=first ele; ---------------------------O(1) |
if(m==0 or n==0)------O(1)-------------------| |--O(n*(4+1))=O(5n)
n+=m+1; ------------O(1) | |
else{ |--O(4) |
s.add(--m); -------O(1) | |
s.add(++m); -------O(1) | |
n--; ----------O(1)--------------------|----------------------|
}
}
return n;
}

Complexity: O(5n+2)
9.Given a positive integer determine whether it is the sum of all of
its divisors

n-> the given integer

i=2,sum=0 ----------O(1)
while(i<=n){ -------O(n)--------|
if(n%i==0) -----------O(1) |
sum += i -----------O(2) |---O(n*(1+2+1))=O(4n)
increment i }----------O(1) -------|
if(sum==n)--------|
true |-------O(1)
else |
false---------|
Complexity: O(1+4n+1) = O(4n+2)

10.GCD of 2 numbers

a and b --> two numbers whose gcd needs to be calculated

gcd(a,b){
if a mod b = 0 then return b -------O(2)
gcd(b,a mod b)--------------------------O(log a)

Complexity: O(log(a)+2)

You might also like