ADS(16125)
ADS(16125)
#include <iostream>
int temp = a;
a = b;
b = temp;
// RANDOMIZED-PARTITION function
int i = p - 1;
comparisonCount++;
i++;
swap(arr[i], arr[j]);
return i + 1;
// RANDOMIZED-QUICKSORT function
if (p < r) {
randomizedQuickSort(arr, p, q - 1, comparisonCount);
randomizedQuickSort(arr, q + 1, r, comparisonCount);
int main() {
int n;
int arr[n];
int comparisonCount = 0;
randomizedQuickSort(arr, 0, n - 1, comparisonCount);
return 0;
#include <iostream>
#include <cstdlib>
int temp = a;
a = b;
b = temp;
// RANDOMIZED-PARTITION function
// PARTITION function
int i = p;
i++;
swap(arr[i], arr[j]);
swap(arr[p], arr[i]);
return i;
// RANDOMIZED-SELECT function
return arr[p];
int k = q - p + 1;
return arr[q];
} else if (i < k) {
} else {
int main() {
int n, i;
cin >> n;
int arr[n];
cout << "Enter the value of i (to find the ith smallest element): ";
cin >> i;
if (i < 1 || i > n) {
cout << "Invalid value of i. It must be between 1 and " << n << "." << endl;
return 1;
cout << "The " << i << "th smallest element is: " << ithSmallest << endl;
return 0;
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
class Edge {
public:
};
class Graph {
public:
int V, E;
vector<Edge> edges;
Graph(int V, int E) {
this->V = V;
this->E = E;
edges.push_back(edge);
class DisjointSets {
public:
DisjointSets(int n) {
parent.resize(n);
rank.resize(n, 0);
parent[i] = i;
int find(int u) {
if (u != parent[u])
parent[u] = find(parent[u]);
return parent[u];
}
void unionSets(int u, int v) {
parent[rootU] = rootV;
parent[rootV] = rootU;
else {
parent[rootV] = rootU;
rank[rootU]++;
};
void kruskalMST() {
[](Edge a, Edge b) {
});
Q.push(e);
DisjointSets forest(V);
vector<Edge> result;
Q.pop();
int u = forest.find(e.src);
int v = forest.find(e.dest);
if (u != v) {
result.push_back(e);
forest.unionSets(u, v);
cout << edge.src << " -- " << edge.dest << " == " << edge.weight << endl;
};
int main() {
graph.addEdge(0, 1, 10);
graph.addEdge(0, 2, 6);
graph.addEdge(0, 3, 5);
graph.addEdge(1, 3, 15);
graph.addEdge(2, 3, 4);
graph.kruskalMST();
return 0;
#include <iostream>
#include <vector>
#include <climits>
class Edge {
public:
};
class Graph {
public:
int V, E;
vector<Edge> edges;
Graph(int V, int E) {
this->V = V;
this->E = E;
}
void addEdge(int src, int dest, int weight) {
edges.push_back(edge);
dist[src] = 0;
int u = edge.src;
int v = edge.dest;
int u = edge.src;
int v = edge.dest;
return;
}
}
printSolution(dist);
};
int main() {
graph.addEdge(0, 1, -1);
graph.addEdge(0, 2, 4);
graph.addEdge(1, 2, 3);
graph.addEdge(1, 3, 2);
graph.addEdge(1, 4, 2);
graph.addEdge(3, 2, 5);
graph.addEdge(3, 1, 1);
graph.addEdge(4, 3, -3);
int source = 0;
graph.bellmanFord(source);
return 0;
Que 5: B-Tree
#include <iostream>
class BTreeNode {
int *keys;
int t;
BTreeNode **C;
int n;
bool leaf;
public:
void traverse();
};
class BTree {
BTreeNode *root;
int t;
public:
BTree(int _t) {
root = nullptr;
t = _t;
void traverse() {
BTreeNode *search(int k) {
};
t = _t;
leaf = _leaf;
n = 0;
}
void BTreeNode::traverse() {
int i;
if (leaf == false) {
C[i]->traverse();
if (leaf == false) {
C[i]->traverse();
BTreeNode *BTreeNode::search(int k) {
int i = 0;
i++;
if (keys[i] == k) {
return this;
if (leaf == true) {
return nullptr;
}
return C[i]->search(k);
void BTree::insert(int k) {
if (root == nullptr) {
root->keys[0] = k;
root->n = 1;
} else {
if (root->n == 2 * t - 1) {
s->C[0] = root;
s->splitChild(0, root);
int i = 0;
if (s->keys[0] < k) {
i++;
s->C[i]->insertNonFull(k);
root = s;
} else {
root->insertNonFull(k);
}
void BTreeNode::insertNonFull(int k) {
int i = n - 1;
if (leaf == true) {
keys[i + 1] = keys[i];
i--;
keys[i + 1] = k;
n = n + 1;
} else {
i--;
if (C[i + 1]->n == 2 * t - 1) {
if (keys[i + 1] < k) {
i++;
C[i + 1]->insertNonFull(k);
if (y->leaf == false) {
y->n = t - 1;
C[j + 1] = C[j];
C[i + 1] = z;
keys[j + 1] = keys[j];
n = n + 1;
}
int main() {
BTree t(3);
t.insert(10);
t.insert(20);
t.insert(5);
t.insert(6);
t.insert(12);
t.insert(30);
t.insert(7);
t.insert(17);
t.traverse();
return 0;
#include <iostream>
class Node {
public:
int key;
Node(int item) {
key = item;
};
class BinaryTree {
public:
Node *root;
BinaryTree() {
root = nullptr;
void inorder() {
inorderRec(root);
private:
if (root == nullptr) {
return root;
return root;
if (root != nullptr) {
inorderRec(root->left);
inorderRec(root->right);
}
};
int main() {
BinaryTree tree;
tree.insert(50);
tree.insert(30);
tree.insert(20);
tree.insert(40);
tree.insert(70);
tree.insert(60);
tree.insert(80);
tree.inorder();
if (tree.search(key) != nullptr) {
cout << "\nNode " << key << " found in the tree." << endl;
} else {
cout << "\nNode " << key << " not found in the tree." << endl;
key = 25;
if (tree.search(key) != nullptr) {
cout << "Node " << key << " found in the tree." << endl;
} else {
cout << "Node " << key << " not found in the tree." << endl;
}
return 0;
#include <iostream>
#include <vector>
#include <string>
int length = 0;
lps[0] = 0;
int i = 1;
while (i < M) {
if (pattern[i] == pattern[length]) {
length++;
lps[i] = length;
i++;
} else {
if (length != 0) {
} else {
lps[i] = 0;
i++;
}
int M = pattern.size();
int N = text.size();
vector<int> lps(M);
computeLPSArray(pattern, M, lps);
int i = 0;
int j = 0;
while (i < N) {
if (pattern[j] == text[i]) {
i++;
j++;
if (j == M) {
j = lps[j - 1];
if (j != 0) {
j = lps[j - 1];
} else {
i++;
}
}
int main() {
KMPSearch(pattern, text);
return 0;
Que 8: Su ix Tree
#include <iostream>
#include <vector>
#include <unordered_map>
#include <string>
class Su ixTreeNode {
public:
int start;
int* end;
int su ixIndex;
class Su ixTree {
private:
string text;
Su ixTreeNode* root;
int size;
void buildTree() {
int n = text.size();
int i = 0;
currentEnd = i;
remainingSu ixCount++;
lastNewNode = -1;
if (currentNode->children.find(text[i]) == currentNode->children.end()) {
if (lastNewNode != -1) {
lastNewNode->su ixIndex = i;
lastNewNode = -1;
} else {
if (nextNode->start < i) {
currentNode = nextNode;
break;
public:
size = text.size();
buildTree();
cout << "Leaf at index " << node->su ixIndex << " with substring: ";
void display() {
printTree(root);
};
int main() {
string inputText;
Su ixTree su ixTree(inputText);
";
su ixTree.display();
return 0;