addition from WHO
This commit is contained in:
1 parent
5ebfed9781
commit
2c87094755
11 files changed
+529
No files matched your search
@@ -0,0 +1,54 @@
|
|||||||
|
#include <iostream>
|
||||||
|
#include <vector>
|
||||||
|
#include <queue>
|
||||||
|
#include <list>
|
||||||
|
|
||||||
|
class Graph {
|
||||||
|
public:
|
||||||
|
Graph(int vertices) : adjList(vertices) {}
|
||||||
|
|
||||||
|
void addEdge(int u, int v) {
|
||||||
|
adjList[u].push_back(v); // Add v to u's list
|
||||||
|
adjList[v].push_back(u); // Because it's an undirected graph
|
||||||
|
}
|
||||||
|
|
||||||
|
void bfs(int start) {
|
||||||
|
std::vector<bool> visited(adjList.size(), false);
|
||||||
|
std::queue<int> q;
|
||||||
|
visited[start] = true;
|
||||||
|
q.push(start);
|
||||||
|
|
||||||
|
while (!q.empty()) {
|
||||||
|
int node = q.front();
|
||||||
|
q.pop();
|
||||||
|
std::cout << node << " "; // Process the current node
|
||||||
|
|
||||||
|
// Visit all the unvisited neighbors
|
||||||
|
for (int neighbor : adjList[node]) {
|
||||||
|
if (!visited[neighbor]) {
|
||||||
|
visited[neighbor] = true;
|
||||||
|
q.push(neighbor);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::vector<std::list<int>> adjList; // Adjacency list representation of the graph
|
||||||
|
};
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
Graph g(6); // Create a graph with 6 vertices
|
||||||
|
|
||||||
|
g.addEdge(0, 1);
|
||||||
|
g.addEdge(0, 2);
|
||||||
|
g.addEdge(1, 3);
|
||||||
|
g.addEdge(1, 4);
|
||||||
|
g.addEdge(2, 5);
|
||||||
|
|
||||||
|
std::cout << "BFS starting from node 0: ";
|
||||||
|
g.bfs(0); // Perform BFS starting from node 0
|
||||||
|
std::cout << std::endl;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,37 @@
|
|||||||
|
#include <iostream>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
int binarySearch(const std::vector<int>& arr, int target) {
|
||||||
|
int low = 0;
|
||||||
|
int high = arr.size() - 1;
|
||||||
|
|
||||||
|
while (low <= high) {
|
||||||
|
int mid = low + (high - low) / 2;
|
||||||
|
|
||||||
|
if (arr[mid] == target) {
|
||||||
|
return mid; // Element found at index `mid`
|
||||||
|
}
|
||||||
|
if (arr[mid] < target) {
|
||||||
|
low = mid + 1; // Search in the right half
|
||||||
|
} else {
|
||||||
|
high = mid - 1; // Search in the left half
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return -1; // Element not found
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
std::vector<int> arr = {1, 2, 3, 4, 5, 6, 7, 8, 9};
|
||||||
|
|
||||||
|
int target = 5;
|
||||||
|
int index = binarySearch(arr, target);
|
||||||
|
|
||||||
|
if (index != -1) {
|
||||||
|
std::cout << "Element " << target << " found at index " << index << std::endl;
|
||||||
|
} else {
|
||||||
|
std::cout << "Element " << target << " not found" << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,128 @@
|
|||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
// Define the structure for a binary tree node
|
||||||
|
struct Node {
|
||||||
|
int data; // Value stored in the node
|
||||||
|
Node* left; // Pointer to the left child
|
||||||
|
Node* right; // Pointer to the right child
|
||||||
|
|
||||||
|
Node(int value) : data(value), left(nullptr), right(nullptr) {} // Constructor
|
||||||
|
};
|
||||||
|
|
||||||
|
// Binary Tree class definition
|
||||||
|
class BinaryTree {
|
||||||
|
public:
|
||||||
|
Node* root; // Root of the binary tree
|
||||||
|
|
||||||
|
BinaryTree() : root(nullptr) {} // Constructor to initialize the root to null
|
||||||
|
|
||||||
|
// Insert a new node in the binary tree
|
||||||
|
void insert(int value) {
|
||||||
|
root = insertRec(root, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
// In-order traversal (left, root, right)
|
||||||
|
void inOrder() {
|
||||||
|
inOrderRec(root);
|
||||||
|
std::cout << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Pre-order traversal (root, left, right)
|
||||||
|
void preOrder() {
|
||||||
|
preOrderRec(root);
|
||||||
|
std::cout << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Post-order traversal (left, right, root)
|
||||||
|
void postOrder() {
|
||||||
|
postOrderRec(root);
|
||||||
|
std::cout << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Search for a value in the binary tree
|
||||||
|
bool search(int value) {
|
||||||
|
return searchRec(root, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
// Helper function for inserting a node recursively
|
||||||
|
Node* insertRec(Node* node, int value) {
|
||||||
|
// If the tree is empty, create a new node
|
||||||
|
if (node == nullptr) {
|
||||||
|
return new Node(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Otherwise, recur down the tree
|
||||||
|
if (value < node->data) {
|
||||||
|
node->left = insertRec(node->left, value); // Insert in the left subtree
|
||||||
|
} else {
|
||||||
|
node->right = insertRec(node->right, value); // Insert in the right subtree
|
||||||
|
}
|
||||||
|
|
||||||
|
return node;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Helper function for in-order traversal (left, root, right)
|
||||||
|
void inOrderRec(Node* node) {
|
||||||
|
if (node == nullptr) return;
|
||||||
|
inOrderRec(node->left); // Visit left subtree
|
||||||
|
std::cout << node->data << " "; // Visit root
|
||||||
|
inOrderRec(node->right); // Visit right subtree
|
||||||
|
}
|
||||||
|
|
||||||
|
// Helper function for pre-order traversal (root, left, right)
|
||||||
|
void preOrderRec(Node* node) {
|
||||||
|
if (node == nullptr) return;
|
||||||
|
std::cout << node->data << " "; // Visit root
|
||||||
|
preOrderRec(node->left); // Visit left subtree
|
||||||
|
preOrderRec(node->right); // Visit right subtree
|
||||||
|
}
|
||||||
|
|
||||||
|
// Helper function for post-order traversal (left, right, root)
|
||||||
|
void postOrderRec(Node* node) {
|
||||||
|
if (node == nullptr) return;
|
||||||
|
postOrderRec(node->left); // Visit left subtree
|
||||||
|
postOrderRec(node->right); // Visit right subtree
|
||||||
|
std::cout << node->data << " "; // Visit root
|
||||||
|
}
|
||||||
|
|
||||||
|
// Helper function for searching a value in the tree
|
||||||
|
bool searchRec(Node* node, int value) {
|
||||||
|
if (node == nullptr) return false;
|
||||||
|
|
||||||
|
if (node->data == value) return true; // Value found
|
||||||
|
else if (value < node->data) return searchRec(node->left, value); // Search in left subtree
|
||||||
|
else return searchRec(node->right, value); // Search in right subtree
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Driver function to test the binary tree implementation
|
||||||
|
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);
|
||||||
|
|
||||||
|
std::cout << "In-order traversal: ";
|
||||||
|
tree.inOrder(); // Output: 20 30 40 50 60 70 80
|
||||||
|
|
||||||
|
std::cout << "Pre-order traversal: ";
|
||||||
|
tree.preOrder(); // Output: 50 30 20 40 70 60 80
|
||||||
|
|
||||||
|
std::cout << "Post-order traversal: ";
|
||||||
|
tree.postOrder(); // Output: 20 40 30 60 80 70 50
|
||||||
|
|
||||||
|
int searchValue = 60;
|
||||||
|
if (tree.search(searchValue)) {
|
||||||
|
std::cout << "Found " << searchValue << " in the tree!" << std::endl;
|
||||||
|
} else {
|
||||||
|
std::cout << searchValue << " not found in the tree." << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
#include <iostream>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
void bubbleSort(std::vector<int>& arr) {
|
||||||
|
int n = arr.size();
|
||||||
|
bool swapped;
|
||||||
|
for (int i = 0; i < n - 1; ++i) {
|
||||||
|
swapped = false;
|
||||||
|
for (int j = 0; j < n - i - 1; ++j) {
|
||||||
|
if (arr[j] > arr[j + 1]) {
|
||||||
|
std::swap(arr[j], arr[j + 1]);
|
||||||
|
swapped = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!swapped) break; // If no elements were swapped, the array is sorted
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
std::vector<int> arr = {5, 2, 9, 1, 5, 6};
|
||||||
|
bubbleSort(arr);
|
||||||
|
for (int num : arr) {
|
||||||
|
std::cout << num << " ";
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,49 @@
|
|||||||
|
#include <iostream>
|
||||||
|
#include <vector>
|
||||||
|
#include <list>
|
||||||
|
|
||||||
|
class Graph {
|
||||||
|
public:
|
||||||
|
Graph(int vertices) : adjList(vertices) {}
|
||||||
|
|
||||||
|
void addEdge(int u, int v) {
|
||||||
|
adjList[u].push_back(v); // Add v to u's list
|
||||||
|
adjList[v].push_back(u); // Because it's an undirected graph
|
||||||
|
}
|
||||||
|
|
||||||
|
void dfs(int start) {
|
||||||
|
std::vector<bool> visited(adjList.size(), false);
|
||||||
|
dfsUtil(start, visited);
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
void dfsUtil(int node, std::vector<bool>& visited) {
|
||||||
|
visited[node] = true;
|
||||||
|
std::cout << node << " "; // Process the current node
|
||||||
|
|
||||||
|
// Visit all the unvisited neighbors
|
||||||
|
for (int neighbor : adjList[node]) {
|
||||||
|
if (!visited[neighbor]) {
|
||||||
|
dfsUtil(neighbor, visited);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<std::list<int>> adjList; // Adjacency list representation of the graph
|
||||||
|
};
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
Graph g(6); // Create a graph with 6 vertices
|
||||||
|
|
||||||
|
g.addEdge(0, 1);
|
||||||
|
g.addEdge(0, 2);
|
||||||
|
g.addEdge(1, 3);
|
||||||
|
g.addEdge(1, 4);
|
||||||
|
g.addEdge(2, 5);
|
||||||
|
|
||||||
|
std::cout << "DFS starting from node 0: ";
|
||||||
|
g.dfs(0); // Perform DFS starting from node 0
|
||||||
|
std::cout << std::endl;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,56 @@
|
|||||||
|
#include <iostream>
|
||||||
|
#include <vector>
|
||||||
|
#include <list>
|
||||||
|
#include <stack>
|
||||||
|
|
||||||
|
class Graph {
|
||||||
|
public:
|
||||||
|
Graph(int vertices) : adjList(vertices) {}
|
||||||
|
|
||||||
|
void addEdge(int u, int v) {
|
||||||
|
adjList[u].push_back(v);
|
||||||
|
adjList[v].push_back(u);
|
||||||
|
}
|
||||||
|
|
||||||
|
void dfsIterative(int start) {
|
||||||
|
std::vector<bool> visited(adjList.size(), false);
|
||||||
|
std::stack<int> s;
|
||||||
|
s.push(start);
|
||||||
|
|
||||||
|
while (!s.empty()) {
|
||||||
|
int node = s.top();
|
||||||
|
s.pop();
|
||||||
|
|
||||||
|
if (!visited[node]) {
|
||||||
|
visited[node] = true;
|
||||||
|
std::cout << node << " "; // Process the node
|
||||||
|
}
|
||||||
|
|
||||||
|
// Push unvisited neighbors onto the stack
|
||||||
|
for (int neighbor : adjList[node]) {
|
||||||
|
if (!visited[neighbor]) {
|
||||||
|
s.push(neighbor);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::vector<std::list<int>> adjList; // Adjacency list representation
|
||||||
|
};
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
Graph g(6);
|
||||||
|
|
||||||
|
g.addEdge(0, 1);
|
||||||
|
g.addEdge(0, 2);
|
||||||
|
g.addEdge(1, 3);
|
||||||
|
g.addEdge(1, 4);
|
||||||
|
g.addEdge(2, 5);
|
||||||
|
|
||||||
|
std::cout << "Iterative DFS starting from node 0: ";
|
||||||
|
g.dfsIterative(0);
|
||||||
|
std::cout << std::endl;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,41 @@
|
|||||||
|
#include <iostream>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
void heapify(std::vector<int>& arr, int n, int i) {
|
||||||
|
int largest = i;
|
||||||
|
int left = 2 * i + 1;
|
||||||
|
int right = 2 * i + 2;
|
||||||
|
|
||||||
|
if (left < n && arr[left] > arr[largest])
|
||||||
|
largest = left;
|
||||||
|
|
||||||
|
if (right < n && arr[right] > arr[largest])
|
||||||
|
largest = right;
|
||||||
|
|
||||||
|
if (largest != i) {
|
||||||
|
std::swap(arr[i], arr[largest]);
|
||||||
|
heapify(arr, n, largest);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void heapSort(std::vector<int>& arr) {
|
||||||
|
int n = arr.size();
|
||||||
|
|
||||||
|
for (int i = n / 2 - 1; i >= 0; --i) {
|
||||||
|
heapify(arr, n, i);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = n - 1; i >= 0; --i) {
|
||||||
|
std::swap(arr[0], arr[i]);
|
||||||
|
heapify(arr, i, 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
std::vector<int> arr = {12, 11, 13, 5, 6, 7};
|
||||||
|
heapSort(arr);
|
||||||
|
for (int num : arr) {
|
||||||
|
std::cout << num << " ";
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
#include <iostream>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
void insertionSort(std::vector<int>& arr) {
|
||||||
|
int n = arr.size();
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
arr[j + 1] = key;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
std::vector<int> arr = {12, 11, 13, 5, 6};
|
||||||
|
insertionSort(arr);
|
||||||
|
for (int num : arr) {
|
||||||
|
std::cout << num << " ";
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,58 @@
|
|||||||
|
#include <iostream>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
void merge(std::vector<int>& arr, int left, int mid, int right) {
|
||||||
|
int n1 = mid - left + 1;
|
||||||
|
int n2 = right - mid;
|
||||||
|
|
||||||
|
std::vector<int> L(n1), R(n2);
|
||||||
|
|
||||||
|
for (int i = 0; i < n1; ++i) {
|
||||||
|
L[i] = arr[left + i];
|
||||||
|
}
|
||||||
|
for (int j = 0; j < n2; ++j) {
|
||||||
|
R[j] = arr[mid + 1 + j];
|
||||||
|
}
|
||||||
|
|
||||||
|
int i = 0, j = 0, k = left;
|
||||||
|
while (i < n1 && j < n2) {
|
||||||
|
if (L[i] <= R[j]) {
|
||||||
|
arr[k] = L[i];
|
||||||
|
++i;
|
||||||
|
} else {
|
||||||
|
arr[k] = R[j];
|
||||||
|
++j;
|
||||||
|
}
|
||||||
|
++k;
|
||||||
|
}
|
||||||
|
|
||||||
|
while (i < n1) {
|
||||||
|
arr[k] = L[i];
|
||||||
|
++i;
|
||||||
|
++k;
|
||||||
|
}
|
||||||
|
|
||||||
|
while (j < n2) {
|
||||||
|
arr[k] = R[j];
|
||||||
|
++j;
|
||||||
|
++k;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void mergeSort(std::vector<int>& arr, int left, int right) {
|
||||||
|
if (left < right) {
|
||||||
|
int mid = left + (right - left) / 2;
|
||||||
|
mergeSort(arr, left, mid);
|
||||||
|
mergeSort(arr, mid + 1, right);
|
||||||
|
merge(arr, left, mid, right);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
std::vector<int> arr = {38, 27, 43, 3, 9, 82};
|
||||||
|
mergeSort(arr, 0, arr.size() - 1);
|
||||||
|
for (int num : arr) {
|
||||||
|
std::cout << num << " ";
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
#include <iostream>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
int partition(std::vector<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;
|
||||||
|
std::swap(arr[i], arr[j]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
std::swap(arr[i + 1], arr[high]);
|
||||||
|
return (i + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
void quickSort(std::vector<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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
std::vector<int> arr = {10, 7, 8, 9, 1, 5};
|
||||||
|
quickSort(arr, 0, arr.size() - 1);
|
||||||
|
for (int num : arr) {
|
||||||
|
std::cout << num << " ";
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
#include <iostream>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
void selectionSort(std::vector<int>& arr) {
|
||||||
|
int n = arr.size();
|
||||||
|
for (int i = 0; i < n - 1; ++i) {
|
||||||
|
int minIdx = i;
|
||||||
|
for (int j = i + 1; j < n; ++j) {
|
||||||
|
if (arr[j] < arr[minIdx]) {
|
||||||
|
minIdx = j;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
std::swap(arr[i], arr[minIdx]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
std::vector<int> arr = {64, 25, 12, 22, 11};
|
||||||
|
selectionSort(arr);
|
||||||
|
for (int num : arr) {
|
||||||
|
std::cout << num << " ";
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Reference in new issue
Block a user