Chirag ADA fILE
Chirag ADA fILE
BOSE UNIVERSITY OF
SCIENCE & TECHNOLOGY,
YMCA
DEPARTMENT OF COMPUTER
APPLICATIONS
MCA (2024-26)
2nd SEMESTER
Chirag Anand
int main() {
int n;
cout << "Enter the size of array: ";
cin >> n;
int arr[n];
cout << "Enter " << n << " elements: ";
for (int i = 0; i < n; i++)
cin >> arr[i];
cout << "Original array: ";
printArray(arr, n);
selectionSort(arr, n);
2. Insertion Sort
Description: This program sorts an array in ascending order using insertion sort, inserting elements into the
sorted portion.
```cpp
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter the size of array: ";
cin >> n;
int arr[n];
cout << "Enter " << n << " elements: ";
for (int i = 0; i < n; i++)
cin >> arr[i];
cout << "Original array: ";
printArray(arr, n);
insertionSort(arr, n);
cout << "Sorted array: ";
printArray(arr, n);
return 0;
}
```
Sample Input:
Enter the size of array: 5
Enter 5 elements: 64 34 25 12 22
Sample Output:
Original array: 64 34 25 12 22
Sorted array: 12 22 25 34 64
Explanation: Each element is inserted into its correct position in the sorted portion.
3. Merge Sort
Description: This program sorts an array using merge sort, dividing into halves, sorting, and merging.
```cpp
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter the size of array: ";
cin >> n;
int arr[n];
cout << "Enter " << n << " elements: ";
for (int i = 0; i < n; i++)
cin >> arr[i];
cout << "Original array: ";
printArray(arr, n);
mergeSort(arr, 0, n - 1);
cout << "Sorted array: ";
printArray(arr, n);
return 0;
}
```
Sample Input:
Enter the size of array: 5
Enter 5 elements: 64 34 25 12 22
Sample Output:
Original array: 64 34 25 12 22
Sorted array: 12 22 25 34 64
Explanation: Array is divided, sorted, and merged back.
4. Quick Sort
Description: This program sorts an array using quick sort with a pivot-based partition.
```cpp
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter the size of array: ";
cin >> n;
int arr[n];
cout << "Enter " << n << " elements: ";
for (int i = 0; i < n; i++)
cin >> arr[i];
cout << "Original array: ";
printArray(arr, n);
quickSort(arr, 0, n - 1);
cout << "Sorted array: ";
printArray(arr, n);
return 0;
}
```
Sample Input:
Enter the size of array: 5
Enter 5 elements: 64 34 25 12 22
Sample Output:
Original array: 64 34 25 12 22
Sorted array: 12 22 25 34 64
Explanation: Pivot partitions the array, and subarrays are sorted recursively.
5. Heap Sort
Description: This program sorts an array using heap sort with a max heap.
```cpp
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter the size of array: ";
cin >> n;
int arr[n];
6. BFS Traversal
Description: This program performs BFS on a graph using an adjacency matrix.
```cpp
#include <iostream>
#include <queue>
using namespace std;
7. DFS Traversal
Description: This program performs DFS on a graph using an adjacency matrix.
```cpp
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter the number of vertices: ";
cin >> n;
int graph[MAX][MAX] = {0};
cout << "Enter the adjacency matrix (1 for edge, 0 for no edge):\n";
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
cin >> graph[i][j];
int start;
cout << "Enter the starting vertex: ";
cin >> start;
bool visited[MAX] = {false};
cout << "DFS Traversal starting from vertex " << start << ": ";
DFS(graph, start, n, visited);
cout << endl;
return 0;
}
```
Sample Input:
Enter the number of vertices: 4
Enter the adjacency matrix (1 for edge, 0 for no edge):
0110
1001
1001
0110
Enter the starting vertex: 0
Sample Output:
DFS Traversal starting from vertex 0: 0 1 3 2
Explanation: Goes deep into one path before backtracking.
8. Kruskal's Algorithm
Description: This program finds the minimum spanning tree using Kruskal's algorithm.
```cpp
#include <iostream>
using namespace std;
struct Edge {
int src, dest, weight;
};
struct Graph {
int V, E;
Edge* edge;
};
int main() {
int V, E;
cout << "Enter number of vertices: ";
cin >> V;
cout << "Enter number of edges: ";
cin >> E;
Graph* graph = new Graph();
graph->V = V;
graph->E = E;
graph->edge = new Edge[E];
cout << "Enter " << E << " edges (source destination weight):\n";
for (int i = 0; i < E; i++) {
cin >> graph->edge[i].src >> graph->edge[i].dest >> graph->edge[i].weight;
}
KruskalMST(graph);
delete graph->edge;
delete graph;
return 0;
}
```
Sample Input:
Enter number of vertices: 4
Enter number of edges: 5
Enter 5 edges (source destination weight):
0 1 10
026
035
1 3 15
234
Sample Output:
Edges in the Minimum Spanning Tree:
2 -- 3 : 4
0 -- 3 : 5
0 -- 1 : 10
Explanation: MST with total weight 19.
9. Knapsack Problem
Description: This program solves the 0/1 Knapsack problem using dynamic programming.
```cpp
#include <iostream>
using namespace std;
int main() {
int n, capacity;
cout << "Enter number of items: ";
cin >> n;
cout << "Enter knapsack capacity: ";
cin >> capacity;
int values[n], weights[n];
for (int i = 0; i < n; i++) {
cout << "Enter value for item " << i + 1 << ": ";
cin >> values[i];
cout << "Enter weight for item " << i + 1 << ": ";
cin >> weights[i];
}
int maxValue = knapsack(values, weights, n, capacity);
cout << "Maximum value that can be achieved: " << maxValue << endl;
return 0;
}
```
Sample Input:
Enter number of items: 3
Enter knapsack capacity: 50
Enter value for item 1: 60
Enter weight for item 1: 10
Enter value for item 2: 100
Enter weight for item 2: 20
Enter value for item 3: 120
Enter weight for item 3: 30
Sample Output:
Maximum value that can be achieved: 220
Explanation: Selects items 2 and 3 (100 + 120) within capacity.
int tsp(int graph[][10], int n, int visited[], int currPos, int count, int cost, int &answer) {
if (count == n && graph[currPos][0]) {
answer = min(answer, cost + graph[currPos][0]);
return answer;
}
for (int i = 0; i < n; i++) {
if (visited[i] == 0 && graph[currPos][i]) {
visited[i] = 1;
tsp(graph, n, visited, i, count + 1, cost + graph[currPos][i], answer);
visited[i] = 0;
}
}
return answer;
}
int main() {
int n;
cout << "Enter number of cities: ";
cin >> n;
int graph[10][10];
cout << "Enter the distance matrix (0 if no direct path):\n";
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
cin >> graph[i][j];
int visited[10] = {0};
visited[0] = 1;
int answer = INT_MAX;
tsp(graph, n, visited, 0, 1, 0, answer);
cout << "Minimum cost for TSP: " << answer << endl;
return 0;
}
```
Sample Input:
Enter number of cities: 4
Enter the distance matrix (0 if no direct path):
0 10 15 20
10 0 35 25
15 35 0 30
20 25 30 0
Sample Output:
Minimum cost for TSP: 80
Explanation: Shortest path (e.g., 0->1->3->2->0) totals 80.
}
};
}
// function to search the element in the BST
node* search(node* root , int value){
if(root == NULL || root->data == value){
return root;
}
else if(value < root->data ){
return search(root->left,value);
}
return search(root->right , value);
}
int main() {
node* root = NULL;
// Delete a value
root = deletenode(root, 30);
cout << "Inorder after deleting 30: ";
inorder(root);
cout << endl;
return 0;
}
```
Output:
Inorder traversal: 20 30 40 50 60 70 80
Preorder traversal: 50 30 20 40 70 60 80
Postorder traversal: 20 40 30 60 80 70 50
40 found in the BST!
Inorder after deleting 30: 20 40 50 60 70 80
12. Min Max
Description: This program finds the minimum and maximum values in an array.
#include <iostream>
#include <utility> // pair ke liye
using namespace std;
return {min, max}; // Min aur max dono return karne ke liye pair
}
return 0;
} ```
Output:
Minimum: 6
Maximum: 100
13. 8-Queen
**Description:** This program places 8 queens on an 8x8 chessboard with no threats.
```cpp
#include <iostream>
using namespace std;
#define N 8
int main() {
int board[N][N] = {0};
if (solveNQueen(board, 0)) {
cout << "Solution for 8 Queen Problem:\n";
printBoard(board);
} else {
cout << "No solution exists\n";
}
return 0;
}
```
Output :
00100000
10000000
00001000
00000010
00000100
01000000
00010000
00000001
Explanation: One valid placement with no conflicts.
14. Sum of Subset
**Description:** This program finds subsets with a given sum using brute force.
```cpp
#include <iostream>
using namespace std;
int main() {
int arr[] = {3, 34, 4, 12, 5, 2};
int n = sizeof(arr) / sizeof(arr[0]);
int target;
if (isSubsetSum(arr, n, target))
cout << "Subset with given sum exists." << endl;
else
cout << "No subset with given sum." << endl;
return 0;
}
```
Output :
Enter target sum: 15
Subset with given sum exists.