Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added Algorithms/DFS
Binary file not shown.
65 changes: 65 additions & 0 deletions Algorithms/DFS.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#include <bits/stdc++.h>
using namespace std;

void dfs_recursive(int u, const vector<vector<int>>& adj, vector<bool>& vis, vector<int>& order) {
vis[u] = true;
order.push_back(u);
for (int v : adj[u]) if (!vis[v]) dfs_recursive(v, adj, vis, order);
}

vector<int> dfs_iterative(int start, const vector<vector<int>>& adj) {
int n = adj.size();
vector<bool> vis(n, false);
vector<int> order;
stack<int> 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<vector<int>> 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<bool> vis(n, false);
vector<int> order_rec;
dfs_recursive(start, adj, vis, order_rec);

cout << "Recursive DFS order: ";
for (int x : order_rec) cout << x << ' ';
cout << '\n';

vector<int> order_it = dfs_iterative(start, adj);
cout << "Iterative DFS order: ";
for (int x : order_it) cout << x << ' ';
cout << '\n';

return 0;
}
Binary file added Algorithms/Quick_Sort
Binary file not shown.
53 changes: 53 additions & 0 deletions Algorithms/Quick_Sort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#include <bits/stdc++.h>
using namespace std;

int partition(vector<int>& 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<int>& 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<int>& 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<int> 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;
}