ADA Lab Abhi
ADA Lab Abhi
A230522412
LAB-01
Aim : Implementation of various sorting techniques.
Bubble Sort
Code:
#include <iostream>
using namespace std;
void bubbleSort(int arr[], int n) {
for (int i = 0; i < n-1; i++) {
bool swapped = false;
for (int j = 0; j < n-i-1; j++) {
if (arr[j] > arr[j+1]) {
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
swapped = true; } }
if (!swapped)
break; }}
void printArray(int arr[], int n) {
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
cout << endl; }
int main() {
int n;
cout << "Enter the number of elements: ";
cin >> n;
int arr[n];
cout << "Enter the elements: ";
for (int i = 0; i < n; i++) {
cin >> arr[i]; }
bubbleSort(arr, n);
cout << "Sorted array: ";
printArray(arr, n);
ABHISHEK KUMAR
A230522412
return 0; }
Insertion Sort
Code:
#include <iostream>
using namespace std;
void insertionSort(int arr[], int n) {
for (int i = 1; i < n; i++) {
int key = arr[i];
int j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j = j - 1; }
arr[j + 1] = key; } }
void printArray(int arr[], int n) {
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
cout << endl; }
int main() {
int n;
cout << "Enter the number of elements: ";
cin >> n;
int arr[n];
cout << "Enter the elements: ";
for (int i = 0; i < n; i++) {
ABHISHEK KUMAR
A230522412
Selection Sort
Code:
#include <iostream>
using namespace std;
void selectionSort(int arr[], int n) {
for (int i = 0; i < n-1; i++) {
int k = i;
for (int j = i+1; j < n; j++) {
if (arr[j] < arr[k])
k = j; }
int temp = arr[k];
arr[k] = arr[i];
arr[i] = temp; } }
void printArray(int arr[], int n) {
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
cout << endl; }
int main() {
int n;
ABHISHEK KUMAR
A230522412
LAB-02
Aim: Implementation of Binary Search.
Code:
#include <iostream>
using namespace std;
int binarySearch(int arr[], int size, int key) {
int low = 0;
int high = size - 1;
while (low <= high) {
int mid = low + (high - low) / 2;
if (arr[mid] == key)
return mid;
if (arr[mid] < key)
low = mid + 1;
else
high = mid - 1;
}
return -1;
}
int main() {
int size, key;
cout << "Enter the number of elements in the array: ";
cin >> size;
int arr[size];
cout << "Enter the elements in sorted order: ";
for (int i = 0; i < size; i++) {
cin >> arr[i];
}
cout << "Enter the element to search for: ";
cin >> key;
int result = binarySearch(arr, size, key);
if (result != -1)
ABHISHEK KUMAR
A230522412
cout << "Element found at index " << result << endl;
else
cout << "Element not found in the array" << endl;
return 0;
}
ABHISHEK KUMAR
A230522412
LAB-03
Aim: Implementation of Quick Sort and Merge Sort.
Quick Sort
Code:
#include <iostream>
using namespace std;
void swap(int& a, int& b) {
int temp = a;
a = b;
b = temp; }
int partition(int arr[], int low, int high) {
int pivot = arr[high];
int i = (low - 1);
for (int j = low; j <= high - 1; j++) {
if (arr[j] < pivot) {
i++;
swap(arr[i], arr[j]); } }
swap(arr[i + 1], arr[high]);
return (i + 1); }
void quickSort(int arr[], int low, int high) {
if (low < high) {
int pi = partition(arr, low, high);
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high); } }
void printArray(int arr[], int n) {
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
cout << endl; }
int main() {
int n;
cout << "Enter the number of elements: ";
cin >> n;
ABHISHEK KUMAR
A230522412
int arr[n];
cout << "Enter the elements: ";
for (int i = 0; i < n; i++) {
cin >> arr[i]; }
quickSort(arr, 0, n - 1);
cout << "Sorted array: ";
printArray(arr, n);
return 0; }
Merge Sort
Code:
#include <iostream>
using namespace std;
void merge(int arr[], int left, int mid, int right) {
int n1 = mid - left + 1;
int n2 = right - mid;
LAB-04
Aim: V Strassen Multiplication
Code:
#include <iostream>
using namespace std;
void strassenMultiply(int A[2][2], int B[2][2], int C[2][2]) {
int P1 = (A[0][0] + A[1][1]) * (B[0][0] + B[1][1]);
int P2 = (A[1][0] + A[1][1]) * B[0][0];
int P3 = A[0][0] * (B[0][1] - B[1][1]);
int P4 = A[1][1] * (B[1][0] - B[0][0]);
int P5 = (A[0][0] + A[0][1]) * B[1][1];
int P6 = (A[1][0] - A[0][0]) * (B[0][0] + B[0][1]);
int P7 = (A[0][1] - A[1][1]) * (B[1][0] + B[1][1]);
C[0][0] = P1 + P4 - P5 + P7;
C[0][1] = P3 + P5;
C[1][0] = P2 + P4;
C[1][1] = P1 + P3 - P2 + P6; }
int main() {
int A[2][2], B[2][2], C[2][2];
cout << "Enter elements of 2x2 matrix A (row-wise):\n";
for (int i = 0; i < 2; i++)
for (int j = 0; j < 2; j++)
cin >> A[i][j];
cout << "Enter elements of 2x2 matrix B (row-wise):\n";
for (int i = 0; i < 2; i++)
for (int j = 0; j < 2; j++)
cin >> B[i][j];
strassenMultiply(A, B, C);
cout << "Resultant matrix C after Strassen's multiplication:\n";
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
cout << C[i][j] << " "; }
ABHISHEK KUMAR
A230522412
LAB-05
Aim: Find Shortest Paths to other vertices using Dijkstra’s Algorithm.
Code:
#include <iostream>
using namespace std;
#define INF 99999
int findMinVertex(int dist[], bool visited[], int V) {
int minVertex = -1;
for (int i = 0; i < V; i++) {
if (!visited[i] && (minVertex == -1 || dist[i] < dist[minVertex])) {
minVertex = i; } }
return minVertex; }
void dijkstra(int graph[10][10], int V, int src) {
int dist[V];
bool visited[V];
for (int i = 0; i < V; i++) {
dist[i] = INF;
visited[i] = false; }
dist[src] = 0;
for (int i = 0; i < V - 1; i++) {
int minVertex = findMinVertex(dist, visited, V);
visited[minVertex] = true;
for (int j = 0; j < V; j++) {
if (graph[minVertex][j] != 0 && !visited[j]) {
int newDist = dist[minVertex] + graph[minVertex][j];
if (newDist < dist[j]) {
dist[j] = newDist; } } } }
cout << "Vertex\tDistance from Source\n";
for (int i = 0; i < V; i++) {
cout << i << "\t" << dist[i] << endl; }
}
int main() {
ABHISHEK KUMAR
A230522412
int V;
cout << "Enter the number of vertices: ";
cin >> V;
int graph[10][10];
cout << "Enter the adjacency matrix (enter 0 for no edge):\n";
for (int i = 0; i < V; i++) {
for (int j = 0; j < V; j++) {
cin >> graph[i][j]; } }
int src;
cout << "Enter the source vertex: ";
cin >> src;
dijkstra(graph, V, src);
return 0;
}
ABHISHEK KUMAR
A230522412
LAB-06
Aim: Implementation of Bellman Ford.
Code:
#include <iostream>
#include <vector>
#include <limits.h>
distance[start] = 0;
int u = graph[j][0];
int v = graph[j][1];
int u = graph[j][0];
int v = graph[j][1];
return; }}
if (distance[i] == INT_MAX) {
} else {
int main() {
return 0;
}
ABHISHEK KUMAR
A230522412
LAB-07
Aim: Implementing Fractional knapsack and 0/1 knapsack problem using
Dynamic Programming.
Fractional Knapsack Method
Code:
#include <iostream>
#include <algorithm>
using namespace std;
#define MAX_ITEMS 100
int main() {
int n;
cout << "Enter the number of items: ";
cin >> n;
swap(ratio[i], ratio[j]);
swap(values[i], values[j]);
swap(weights[i], weights[j]); } }}
double maxValue = 0.0;
ABHISHEK KUMAR
A230522412
else {
dp[i][w] = dp[i - 1][w];
}
}
}
return dp[n][W];
}
int main() {
int n;
cout << "Enter the number of items: ";
cin >> n;
int val[n], wt[n];
int W;
cout << "Enter the values of the items:\n";
for (int i = 0; i < n; i++) {
cin >> val[i]; }
cout << "Enter the weights of the items:\n";
for (int i = 0; i < n; i++) {
cin >> wt[i]; }
cout << "Enter the maximum capacity of the knapsack: ";
cin >> W;
cout << "The maximum value that can be obtained is: " << knapsack(W, wt, val, n) << endl;
return 0;
}
ABHISHEK KUMAR
A230522412
LAB-08
Aim: Implement Longest Common Subsequence.
Code:
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int LCS(string s1, string s2) {
int n = s1.length();
int m = s2.length();
vector<vector<int>> dp(n + 1, vector<int>(m + 1, 0));
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (s1[i - 1] == s2[j - 1]) {
dp[i][j] = dp[i - 1][j - 1] + 1; // If characters match
} else {
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
}
}
}
return dp[n][m];
}
int main() {
string s1, s2;
cout << "Enter the first string: ";
cin >> s1;
cout << "Enter the second string: ";
cin >> s2;
int lcsLength = LCS(s1, s2);
cout << "Length of LCS: " << lcsLength << endl;
return 0;
}
ABHISHEK KUMAR
A230522412
ABHISHEK KUMAR
A230522412
LAB-09
Aim: Implement Traveling Salesman Problem using Dynamic Programming.
Code:
#include <iostream>
#include <vector>
using namespace std;
const int INF = 999999;
int main() {
int n;
cout << "Enter the number of cities: ";
cin >> n;
int dist[10][10];
cout << "Enter the distance matrix:\n";
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> dist[i][j];
}
}
vector<vector<int>> dp(1 << n, vector<int>(n, INF));
dp[1][0] = 0;
for (int mask = 1; mask < (1 << n); mask++) {
for (int i = 0; i < n; i++) {
if (mask & (1 << i)) {
for (int j = 0; j < n; j++) {
if (mask & (1 << j)) {
dp[mask][i] = min(dp[mask][i], dp[mask ^ (1 << i)][j] + dist[j][i]);
}
}
}
}
}
int result = INF;
ABHISHEK KUMAR
A230522412