AashishADS
AashishADS
1st Semester
Session 2024-2026
#include <iostream>
#include <vector>
#include <algorithm>
#include <iostream>
#include <vector>
#include <algorithm> // Bubble Sort (Non-recursive)
std::swapusing namespace std;
void bubbleSort(vector<int>& arr) {
int n = arr.size();
for (int i = 0; i < n - 1; ++i) {
// Flag to detect if any swapping occurredbool swapped = false;
for (int j = 0; j < n - i - 1; ++j) {if (arr[j] > arr[j + 1]) {
swap(arr[j], arr[j + 1]);swapped = true;
}
}
// If no elements were swapped, the array is already sortedif (!swapped) break;
}
}
// Insertion Sort
void insertionSort(vector<int>& arr) {int n = arr.size();
for (int i = 1; i < n; ++i) {int key = arr[i];
int j = i - 1;
// Shift elements that are greater than the key to the rightwhile (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
--j;
}
// Place the key at its correct positionarr[j + 1] = key;
}
}
void quickSort(vector<int>& arr, int low, int high) {if (low < high) {
int pi = partition(arr, low, high); quickSort(arr, low, pi - 1); // Left partition quickSort(arr, pi + 1,
high); // Right partition
}
}
// Bubble Sort
vector<int> bubbleArr = arr;bubbleSort(bubbleArr);
cout << "Bubble Sorted array: ";printArray(bubbleArr);
// Insertion Sort
vector<int> insertionArr = arr;insertionSort(insertionArr);
cout << "Insertion Sorted array: ";printArray(insertionArr);
// Quick Sort
vector<int> quickArr = arr; quickSort(quickArr, 0, quickArr.size() - 1);cout << "Quick Sorted
array: "; printArray(quickArr);
// Merge Sort
vector<int> mergeArr = arr; mergeSort(mergeArr, 0, mergeArr.size() - 1);cout << "Merge Sorted
array: "; printArray(mergeArr);
return 0;
}
Output:-
EXPERIMETS 3
Write a C++ programs that use recursive functions to traverse the given binary
treein:
a)Preorder
b) Inorder
c) Postorder
#include <iostream>
using namespace std;
postorder(root->right);
// Visit the root node cout << root->data << " ";
}
#include <iostream>
#include <vector>
#include <algorithm>
public:
BTree(int _t) : t(_t) {
root = new BTreeNode(t, true);
}
if (leaf) {
// If the node is a leaf, find the position to insert the keywhile (i >= 0 && keys[i] > key) {
i--;
}
keys.insert(keys.begin() + i + 1, key);
} else {
// If the node is not a leaf, find the appropriate child to insert the keywhile (i >= 0 && keys[i] >
key) {
i--;
} i++;
BTreeNode* child = children[i];
// Copy the second half of y's keys to zfor (int j = 0; j < t - 1; j++) {
z->keys[j] = y->keys[j + t];
}
// If y is not a leaf, copy the second half of y's children to zif (!y->leaf) {
z->children.resize(t);
for (int j = 0; j < t; j++) {
z->children[j] = y->children[j + t];
}
}
// Reduce the size of yy->keys.resize(t - 1); y->children.resize(t);
// Move all children of the current node one position aheadchildren.insert(children.begin() + i +
1, z);
// Insert the middle key of y into the current nodekeys.insert(keys.begin() + i, y->keys[t - 1]);
// Clear the middle key from y
y->keys[t - 1] = 0;
}
// Function to print the node and its children (for debugging purposes)void
BTreeNode::printNode() {
// Print all keys in the current node for (int i = 0; i < keys.size(); i++) {
cout << keys[i] << " ";
}
cout << endl;
// If this is not a leaf, recursively print the childrenif (!leaf) {
for (auto child : children) {child->printNode();
}
}
}
int main() {
// Create a B-tree with minimum degree 3 (B-tree of order 6)BTree tree(3);
// Insert keys into the B-treetree.insert(10); tree.insert(20); tree.insert(5); tree.insert(6);
tree.insert(12); tree.insert(30); tree.insert(7); tree.insert(17);
#include <iostream>
#include <vector>
#include <stack>
#include <queue>
#include <set>
public:
Graph(int V); // Constructor to initialize graph
void addEdge(int u, int v); // Function to add an edge to the graph
void BFS(int start); // Function for Breadth First Search
void DFS(int start); // Function for Depth First Search
void DFSUtil(int start, set<int>& visited); // Helper function for DFS
};
visited[start] = true;q.push(start);
cout << "Breadth First Search starting from vertex " << start << ": ";while (!q.empty()) {
int node = q.front();
cout << node << " ";q.pop();
// Visit all the adjacent vertices of the current node
for (int neighbor : adj[node]) {
if (!visited[neighbor]) { visited[neighbor] = true;
q.push(neighbor);
}
}
}
cout << endl;
}