0% found this document useful (0 votes)
5 views18 pages

Daa 2

The document outlines various classifications of algorithms based on implementation methods, design methods, and other criteria. It also discusses performance analysis of algorithms, including criteria for evaluation such as correctness, documentation, and complexity. Additionally, it provides examples of algorithms for calculating sums, factorials, maximum values, Fibonacci series, and matrix addition using both iterative and recursive approaches.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views18 pages

Daa 2

The document outlines various classifications of algorithms based on implementation methods, design methods, and other criteria. It also discusses performance analysis of algorithms, including criteria for evaluation such as correctness, documentation, and complexity. Additionally, it provides examples of algorithms for calculating sums, factorials, maximum values, Fibonacci series, and matrix addition using both iterative and recursive approaches.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

...

Classification of algorithms

Implementation Method Design Method Other Classifications

1. Recursion or
Iteration 1. Divide and 1. Classification by
2. Procedural or Conquer Research Area
Declarative 2. Greedy Method
2. Classification by
(non- 3. Dynamic
Procedural) Complexity
Programming
3. Serial or Parallel 3. Branch and Bound
or Distributed
4. Linear
Programming & Backtracking
4. Deterministic or
Non- 5. Transform and
4. Randomized
Deterministic Conquer
Algorithms
5. Exact or
Approximate

Algorithm Performance Analysis


• Making evaluative judgment about programs.
• Criteria to judge a program –
– Does the program meet the original specification of the
task.
– Does it work correctly.
– Does the program contain documentation that show how
to use it and how it work.
– Does the program effectively use function to create logical
units.
– Is the program’s code readable.
• It is very difficult to achieve above given criteria. So two
more criteria
– Does the program effectively use primary and secondary
storage. (space complexity)
– Is the program’s running time acceptable for the task.
(time complexity)

1
...

Sum of n numbers using iteration

Sum of n numbers using recursion

2
...

Time analysis

Sum iterative Algorithm with count variable

3
...

Sum recursive Algorithm with count variable

Simple interest calculation using iteration and recursion

// Iterative function to calculate simple interest


1. float Interest(float principal, float rate, float time) {
2. return (principal * rate * time) / 100;
3. }

// Recursive function to calculate simple interest


1. float Interest(float principal, float rate, float time)
2. {
3. if (time == 0) {
4. return 0;
5. }
6. else {
7. return (principal * rate / 100) + Interest(principal, rate, time - 1);
8. }
9. }

4
...

Factorial using Iteration and recursion

1. #include <stdio.h> 1. int factorial(int n) {


2. int factorial(int n) { 2. //Base case: factorial of 0 or 1 is 1
3. if (n == 0) {
3. int result = 1;
4. for (int i = 1; i <= n; i++) { 4. return 1;
5. result *= i; 5. }
6. } 6. // Recursive case: n! = n * (n-1)!
7. return result; 7. else {
8. } 8. return n * factorial(n - 1);
9. }
10. }
Time Complexity
T(n)=2n+3 2, if n = 0
T n =
~ O(n) 1+ T n−1 , if n > 0

Find Max Value using iteration and recursive


1. int Max(int arr[], int n) {
2. // Base case:
1. int Max(int arr[], int n) { 3. if (n == 1) {
2. int max = arr[0];
4. return arr[0];
3. for (int i = 1; i < n; i++) {
5. }
4. if (arr[i] > max) {
6. // Recursive case:
5. max = arr[i];
7. int Rest = Max(arr, n - 1);
6. } 8. if (arr[n - 1] > Rest) {
7. } 9. return arr[n - 1];
8. return max; 10. } else {
9. } 11. return Rest;
12. }
13. }
2, if n = 0
T n =
T n−1 +2 , if n > 0

5
...

Fibonacci Series using Iteration and Recursion

1. void fib (int n) { 1. int fib (int n) {


2. int a = 0, b = 1, next; 2. if (n <= 1) {
3. for (int i = 0; i < n; i++) { 3. return n;
4. if (i <= 1) { 4. }
5. next = i;
5. return fib(n - 1) + fib(n - 2);
6. } else {
6. }
7. next = a + b;
8. a = b; 2, if n = 0 , n = 1
b = next; T n =
9. T n−1 +T n−2 +1 , if n > 1
10. }
12. printf("%d ", next);
13. }
14. printf("\n");
15. }

Matrix Addition using iteration

6
...

1. void addMatrices (int a[m][n], int b[m][n], int result[m][m], int row,
int col)
2. {
1. Base case: if we've processed all rows
1. if (row == SIZE) { return; }
2. Base case: if we've processed all columns in the current row
1. if (col == SIZE)
2. {
3. Move to the next row and reset the column index
4. addMatrices(a, b, result, row + 1, 0);
5. return;
6. }
3. // Add the corresponding elements
1. result[row][col] = a[row][col] + b[row][col];
4. // Move to the next column
1. addMatrices(a, b, result, row, col + 1);
3. }

Running Time Analysis Types

7
...

Machine Dependent Time

8
...

9
...

10
...

Divide and Conquer

11
...

12
...

13
...

14
...

15
...

16
...

17
...

18

You might also like