ADD LAB MANUAL
ADD LAB MANUAL
AIM :
To illustrate and compare various algorithmic complexities, focusing on their order of growth,
from logarithmic to factorial.
ALGORITHM:
Binary Search
Factorial Calculation
2. Recursive Method:
o If n == 0 or n == 1, return 1.
3. Non-Recursive Method:
o Set result to 1.
o For i from 2 to n:
Multiply result by i.
o Return result.
PROGRAM :
#include <iostream>
#include <vector>
#include <algorithm>
int left = 0;
if (arr[mid] == target) {
left = mid + 1;
} else {
right = mid - 1;
if (n <= 1) return 1;
result *= i;
}
return result;
}
void recursivePermutations(string str, string current) {
if (str.empty()) {
cout << current << endl; // Output permutation
} else {
for (size_t i = 0; i < str.size(); ++i) {
string newStr = str;
newStr.erase(i, 1); // Remove character at i
recursivePermutations(newStr, current + str[i]);
}
}
}
int main()
vector<int> sortedArr = {1, 3, 5, 7, 9, 11, 13, 15};
int target = 7;
int index = binarySearch(sortedArr, target);
if (index != -1) {
cout << "Element " << target << " found at index " << index << endl;
} else {
cout << "Element " << target << " not found." << endl;
}
int n = 5; // Change this value for testing
cout << "Factorial of " << n << " (Recursive): " << recursiveFactorial(n) << endl;
cout << "Factorial of " << n << " (Non-Recursive): " << nonRecursiveFactorial(n) << endl;
cout << "Permutations of 'abc':" << endl;
recursivePermutations("abc", "");
return 0;
}
OUTPUT :
Permutations of 'abc':
abc
acb
bac
bca
cab
cba
RESULT :
Thus, the program for recursive and non-recursive algorithm are given above are executed
successfully.
EX.NO : 02
DIVIDE AND CONQUER – STRASSEN’S MATRIX MULTILICATION
DATE :
AIM :
To implement Strassen’s algorithm for efficient matrix multiplication using the divide-and-
conquer approach.
ALGORITHM :
Strassen's Algorithm:
2. Split Matrices:
4. Combine Results:
- C11 = M1 + M4 - M5 + M7
- C12 = M3 + M5
- C21 = M2 + M4
- C22 = M1 + M3 - M2 + M6
5. Combine Quadrants: Combine C11, C12, C21, C22 to form the final product matrix C
PROGRAM :
#include <iostream>
#include <vector>
int n = A.size();
return C;
int n = A.size();
return C;
int n = A.size();
if (n == 1) {
int k = n / 2;
A11[i][j] = A[i][j];
B11[i][j] = B[i][j];
}
}
return C;
int main() {
vector<vector<int>> A = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 16}};
vector<vector<int>> B = {{16, 15, 14, 13}, {12, 11, 10, 9}, {8, 7, 6, 5}, {4, 3, 2, 1}};
printMatrix(A);
printMatrix(B)
printMatrix(C);
return 0;
}
OUTPUT :
Matrix A:
1234
5678
9 10 11 12
13 14 15 16
Matrix B:
16 15 14 13
12 11 10 9
8765
4321
Result of A * B:
80 70 60 50
Thus the above program has been executed successfully and the required output is displayed.