diff --git a/Algorithms/DFS b/Algorithms/DFS new file mode 100755 index 0000000..d6ca9f9 Binary files /dev/null and b/Algorithms/DFS differ diff --git a/Algorithms/DFS.cpp b/Algorithms/DFS.cpp new file mode 100644 index 0000000..e6aa391 --- /dev/null +++ b/Algorithms/DFS.cpp @@ -0,0 +1,65 @@ +#include +using namespace std; + +void dfs_recursive(int u, const vector>& adj, vector& vis, vector& order) { + vis[u] = true; + order.push_back(u); + for (int v : adj[u]) if (!vis[v]) dfs_recursive(v, adj, vis, order); +} + +vector dfs_iterative(int start, const vector>& adj) { + int n = adj.size(); + vector vis(n, false); + vector order; + stack st; + st.push(start); + while (!st.empty()) { + int u = st.top(); st.pop(); + if (vis[u]) continue; + vis[u] = true; + order.push_back(u); + // push neighbors in reverse to mimic recursive order + for (auto it = adj[u].rbegin(); it != adj[u].rend(); ++it) if (!vis[*it]) st.push(*it); + } + return order; +} + +int main() { + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, m; + cout << "DFS (recursive + iterative)\nEnter number of nodes: "; + if (!(cin >> n) || n <= 0) return 0; + cout << "Enter number of edges: "; + cin >> m; + + vector> adj(n); + cout << "Enter edges (u v) 0-based indices, one per line:\n"; + for (int i = 0; i < m; ++i) { + int u, v; cin >> u >> v; + if (u < 0 || u >= n || v < 0 || v >= n) continue; + adj[u].push_back(v); + adj[v].push_back(u); // treat as undirected by default + } + + int start; + cout << "Enter start node: "; + cin >> start; + if (start < 0 || start >= n) start = 0; + + vector vis(n, false); + vector order_rec; + dfs_recursive(start, adj, vis, order_rec); + + cout << "Recursive DFS order: "; + for (int x : order_rec) cout << x << ' '; + cout << '\n'; + + vector order_it = dfs_iterative(start, adj); + cout << "Iterative DFS order: "; + for (int x : order_it) cout << x << ' '; + cout << '\n'; + + return 0; +} diff --git a/Algorithms/Quick_Sort b/Algorithms/Quick_Sort new file mode 100755 index 0000000..b1f463e Binary files /dev/null and b/Algorithms/Quick_Sort differ diff --git a/Algorithms/Quick_Sort.cpp b/Algorithms/Quick_Sort.cpp new file mode 100644 index 0000000..16e6471 --- /dev/null +++ b/Algorithms/Quick_Sort.cpp @@ -0,0 +1,53 @@ +#include +using namespace std; + +int partition(vector& a, int low, int high) { + int pivot = a[high]; // choose last element as pivot + int i = low - 1; + for (int j = low; j < high; ++j) { + if (a[j] <= pivot) { + ++i; + swap(a[i], a[j]); + } + } + swap(a[i + 1], a[high]); + return i + 1; +} + +void quickSort(vector& a, int low, int high) { + if (low < high) { + int pi = partition(a, low, high); + quickSort(a, low, pi - 1); + quickSort(a, pi + 1, high); + } +} + +void printArray(const vector& a) { + for (int v : a) cout << v << " "; + cout << '\n'; +} + +int main() { + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n; + cout << "Quick Sort - Enter number of elements: "; + if (!(cin >> n) || n <= 0) { + cout << "Invalid size\n"; + return 0; + } + + vector a(n); + cout << "Enter " << n << " integers:\n"; + for (int i = 0; i < n; ++i) cin >> a[i]; + + cout << "Original array: "; + printArray(a); + + quickSort(a, 0, n - 1); + + cout << "Sorted array: "; + printArray(a); + return 0; +}