You are on page 1of 7

DAA 5IT-2 19IT088

Design and Analysis of Algorithms


Tutorial Assignment No 1.1
Algorithms Basics and Analysis Techniques

1. Give at least two different real-life examples of a subjective


decision and a non-subjective decision.

 Subjective decision: subjective decisions cannot be proved true or


false by any generally accepted criteria. It often express opinions,
preferences, values, feelings, and judgments.
i. Example: amount of ingredients in a recipe.
ii. Example: editorials in newspaper.

 Non-subjective decision: non-subjective decision based on facts


and truths. It does not express opinions, preferences, values, feelings
and judgments.
i. Example: there are 7 days in a week. ii.
Example: scientific facts.
2.
find the sum of n
elements in an integer array without using recursion.

 Pseudo code:

Sum_of_n_number(array[], n)
Let sum = 0;
For i=0 to n sum=sum
+ array[i];

CSPIT-IT 1|Page
DAA 5IT-2 19IT128

Devise the algorithm and perform the mathematical analysis as


illustrated in the class for the following: To
print sum;

 Analysis:
COST CODE NO. OF TIMES
EXECUTED
C1 Sum=0 1
C2 For i=0 to n n+1
C3 Sum=sum + array[i]; n
C4 Print(sum) 1
 Mathematical analysis for sum of n numbers from
array: Time complexity = O((C1*1)+(C2*n+1)+(C3*n)
+(C4*1))
= O(1+n+1+n+1)
=O(2n+3)
=O(n)
3. perform the bubble
sort.

 Pseudo code:
Bubblesort(array[] n)
Let i=0, temp= 0;
While i<=n-1
For j=0 to j<=n-i
If(array[i]>array[j+1])
temp=array[j] array[j]=array[j+1]
array[j+1]=temp  Analysis:

CSPIT-IT 2|Page
DAA 5IT-2 19IT128

Devise the algorithm and perform the mathematical analysis as


illustrated in the class for the following: To
COST CODE NO. OF TIMES
EXECUTED
C1 i=0, temp= 0; 1
C2 While i<=n-1 n+1
C3 For j=0 to j<=n-i n(n+1)
C4 If(array[i]>array[j+1]) n(n+1)*1
C5 temp=array[j] n(n+1)*1
C6 array[j]=array[j+1] n(n+1)*1
C7 array[j+1]=temp n(n+1)*1
 Mathematical analysis for binary sort:

Time complexity = O((C1*1)+(C2+n+1)+(C3* n(n+1))+(C4* n(n+1)*1)+


(C5* n(n+1)*1)+(C6* n(n+1)*1)+(C7*n(n+1)*1)
= O(5n2+6n+2)
= O(n2)
4. find the smallest
element from an integer array.

 Pseudo code:

Minimum_element(array[],n)
Let minimum = 999;
For i=0 to n if(array[i] <
minimum) minimum =
array[i];
return minimum;

 Analysis:

COST CODE NO. OF TIMES


EXECUTED
C1 minimum = 999 1

CSPIT-IT 3|Page
DAA 5IT-2 19IT128

Devise the algorithm and perform the mathematical analysis as


illustrated in the class for the following: To
C2 For i=0 to n n+1
C3 if(array[i] < minimum) n
C4 minimum = array[i]; n
C5 return minimum; 1
 Mathematical analysis for searching smallest element

Time complexity = O((C1*1)+(C2*n+1)+(C3*n)+(C4*n)+(C5*1))


= O(1+n+1+n+n+1)
= O(3n+3)
= O(n)
5. Devise the algorithm and perform the mathematical analysis as
illustrated in the class for the following: To find the factorial of a
given number without using recursion.

 Pseudo code:
factorial(n)
Let n=5,
fact=1;
For i=1 to n
fact = fact*i;
return fact;

 Analysis:

COST CODE NO. OF TIMES


EXECUTED
C1 n=5, fact=1; 1
C2 For i=1 to n n+1
C3 fact = fact*i; n
C4 return fact; 1

CSPIT-IT 4|Page
DAA 5IT-2 19IT128

Devise the algorithm and perform the mathematical analysis as


illustrated in the class for the following: To
 Mathematical analysis for finding factorial

Time complexity = O((C1*1)+(C2*n+1)+(C3*n)+(C4*1)


= O(1+n+1+n+1)
= O(2n+3)
=O(n)
6. Devise the algorithm and perform the mathematical
analysis as illustrated in the class for the following: To find
the nth Fibonacci number without using recursion.

 Pseudo code:
Fibonacc_numberi(n)
let pre=0,next=1,sum
= 0; print(pre , next)
For i=2 to n sum =
pre+next;
print(sum);
pre = next;
next = sum;

 Analysis:
COST CODE NO. OF TIMES
EXECUTED
C1 pre=0,next=1,sum = 0; 1

CSPIT-IT 5|Page
DAA 5IT-2 19IT128

Devise the algorithm and perform the mathematical analysis as


illustrated in the class for the following: To
C2 For i=2 to n n-1
C3 sum = pre+next; n-2
C4 pre = next; n-2
C5 next = sum; n-2
 Mathematical analysis for Fibonacci number

Time complexity = O((C1*1)+(C2*n-1)+(C3*n-2)+( C4*n-2)+( C5*n-2))


= O(1+n-1+n-2+n-2+n-2)
= O(4n-6)
= O(n)
7. Devise the algorithm and perform the mathematical analysis
as illustrated in the class for the following: To sort an input
array of n integers using insertion sort.

 Pseudo code:
Insertion(array[],n)
For j=1 to n; Key = array[j]; i = j-
1; while( j > 0 and array[i]
> key) array[i+1] =
array[i] i=i-1;
array[i+1] = key;

 Analysis:
COST CODE NO. OF TIMES
EXECUTED
C1 For j=1 to n; n+1
C2 Key = array[j]; n
C3 i = j-1; n

CSPIT-IT 6|Page
DAA 5IT-2 19IT128

Devise the algorithm and perform the mathematical analysis as


illustrated in the class for the following: To
C4 while( j > 0 and array[i] > n*(n+1)
key)
C5 array[i+1] = array[i] n2
C6 i=i-1; n2
C7 array[i+1] = key; 1
 Mathematical analysis for insertion sort

Time complexity = =
O((C1*(n+1))+(C2*n)+(C3*(n))+(C4*(n*(n+1))+(C5*(n2))+(C6 *n2)+(C7*1))
= O(n+1+n+n+n2+n+ n2+ n2+ n2)
= O(4n2+4n+1)
= O(n2)
-

CSPIT-IT 7|Page

You might also like