Daa File
Daa File
Aim: To perform Randomised Quick Sort using array as data structure and analyze its time
complexity.
#include <iostream>
#include <chrono>
#include <cstdlib>
#include <ctime>
using namespace std;
using namespace std::chrono;
#include <iostream>
#include <chrono>
}}}
}}}
if (size == 1) {
return;
A11[i][j] = A[i][j];
B11[i][j] = B[i][j];
B12[i][j] = B[i][j + newSize];
} }
add(P5, P4, tempA, newSize); // Calculating C11, C12, C21, and C22
for (int i = 0; i < newSize; i++) { // Combining results into final matrix C
C[i][j] = C11[i][j];
} }
delete[] tempB[i];
delete[] P1; delete[] P2; delete[] P3; delete[] P4; delete[] P5; delete[] P6; delete[] P7;
}}
int main() {
int n;
cin >> n;
} }
} }
printMatrix(C, n);
cout << "\nTime taken for Strassen's Matrix Multiplication: " << duration.count() << "
microseconds\n";
delete[] A[i];
delete[] B[i];
delete[] C[i];
return 0;}
Experiment-6
Aim: To perform Fractional Knapsack using array as data structure and analyze its time
complexity.
#include <iostream>
#include <algorithm>
using namespace std;
struct Item {
int weight;
int value;
};
bool cmp(struct Item a, struct Item b) {
double r1 = (double)a.value / a.weight;
double r2 = (double)b.value / b.weight;
return r1 > r2;}
double fractionalKnapsack(int m, Item items[], int n) {
sort(items, items + n, cmp);
double totalValue = 0.0;
double x[100];
int U = m;
for (int i = 0; i < n; i++) {
x[i] = 0.0;}
for (int i = 0; i < n; i++) {
if (items[i].weight <= U) {
x[i] = 1.0;
totalValue += items[i].value;
U -= items[i].weight;
} else {
x[i] = (double)U / items[i].weight;
totalValue += x[i] * items[i].value;
break;}}
cout << "Solution vector (fractions of items taken): ";
for (int i = 0; i < n; i++) {
cout << x[i] << " ";}
cout << endl;
return totalValue;
}
int main() {
int n, m;
cout << "Enter the number of items: ";
cin >> n;
Item items[n];
cout << "Enter the capacity of the knapsack: ";
cin >> m;
for (int i = 0; i < n; i++) {
cout << "Enter weight and value for item " << i + 1 << ": ";
cin >> items[i].weight >> items[i].value;}
double maxValue = fractionalKnapsack(m, items, n);
cout << "Maximum value in knapsack = " << maxValue;
cout << "\n CSE-1" << endl;
return 0;
}
Experiment-7
Aim: To implement Binary Search using array as a data structure and analyse its time
complexity.
#include <iostream>
int binary_search(int arr[], int beg, int end, int x) { // Fn to perform binary search on a sorted
array
int mid; // Variable to hold the midpoint of the current search range
if (arr[mid] == x)
if (arr[mid] > x)
else
int main() {
cout << "Enter the size of array: "; // Input the size of the array
cin >> n;
cout << "Enter the elements of the array in sorted order: ";
cout << "Enter the element you want to search in the given array: ";
if (result == -1) {
cout << "The given element " << item << " is not present in the array." << endl;
} else {
cout << "Element " << item << " is present in the array at position " << result + 1 << "."
<< endl; }
cout << "Time taken by binary search: " << duration.count() << " microseconds" << endl;
return 0;
}
EXPERIMENT-9
CODE :
#include <bits/stdc++.h>
using namespace std;
CODE :
#include <iostream>
#include <vector>
#include <climits>
using namespace std;
// Function to perform Matrix Chain Multiplication
int matrixChainMultiplication(vector<int>& dims) {
int n = dims.size() - 1; // Number of matrices
vector<vector<int>> dp(n, vector<int>(n, 0));
for (int len = 2; len <= n; len++) {
for (int i = 0; i < n - len + 1; i++) {
int j = i + len - 1;
dp[i][j] = INT_MAX;
for (int k = i; k < j; k++) {
int cost = dp[i][k] + dp[k + 1][j] + dims[i] * dims[k + 1] * dims[j +
1];
dp[i][j] = min(dp[i][j], cost);
} } }
return dp[0][n - 1];
}
int main() {
int n;
cout << "Enter the number of matrices: ";
cin >> n;
vector<int> dims(n + 1);
cout << "Enter the dimensions of matrices (as an array of size " << n + 1
<< "): ";
for (int i = 0; i <= n; i++) {
cin >> dims[i];
}
int minCost = matrixChainMultiplication(dims);
cout << "Minimum number of multiplications required: " << minCost <<
endl;
return 0;
}