From eeb229e6cf8ba52c9a8d7461ba44d9d466d83d80 Mon Sep 17 00:00:00 2001 From: Rohan Dable <90612195+RohanDable@users.noreply.github.com> Date: Mon, 30 Oct 2023 22:05:36 +0530 Subject: [PATCH] added --- C++/Topological_sort.cpp | 82 +++++++++++++++++++++++++++++++++++++ C++/Tower_Of_Hanoi.cpp | 22 ++++++++++ Selection Sort | 87 ++++++++++++++++++---------------------- 3 files changed, 143 insertions(+), 48 deletions(-) create mode 100644 C++/Topological_sort.cpp create mode 100644 C++/Tower_Of_Hanoi.cpp diff --git a/C++/Topological_sort.cpp b/C++/Topological_sort.cpp new file mode 100644 index 000000000..e83d4d1a2 --- /dev/null +++ b/C++/Topological_sort.cpp @@ -0,0 +1,82 @@ +// C++ Program for topological sort using BFS + +#include +using namespace std; + +bool topological_sort(int n, vector adj[], vector ans) +{ + + vector indegree(n, 0); + + for (int i = 0; i < adj.size(); i++) + indegree[adj[i][1]]++; + + queue q; + + for (int i = 0; i < n; i++) + { + if (indegree[i] == 0) + q.push(i); + } + + if (!q.empty()) + return false; + + while (!q.empty()) + { + + int node = q.front(); + q.pop(); + + ans.push_back(node); + + for (auto it : adj[node]) + { + + indegree[it]--; + + if (indegree[it] == 0) + q.push(it); + } + } + + for (int i = 0; i < n; i++) + { + if (indegree[i] != 0) + return false; + } + + return true; +} + +// Driver program to check above functions +int main() +{ + int n, e; // Input for no. of nodes and edges + cin >> n >> e; + + vector adj[n]; + int u, v; + + for (int i = 0; i < e; i++) + { + cin >> u >> v; + + adj[u].push_back(v); + } + + vector ans; + + bool check = topological_sort(n, adj, ans); // Calling function for topological sort + + if (check == false) + cout << "Not Possible\n"; + else + { + for (auto it : ans) + cout << it << " "; + } + cout << endl; + + return 0; +} diff --git a/C++/Tower_Of_Hanoi.cpp b/C++/Tower_Of_Hanoi.cpp new file mode 100644 index 000000000..2eee096e1 --- /dev/null +++ b/C++/Tower_Of_Hanoi.cpp @@ -0,0 +1,22 @@ +#include +using namespace std; +void toh(int n, char s, char h, char d) +{ + if (n == 1) + { + cout << "Move disk " << n << " from rod " << s << " to rod " << d << endl; + return; + } + toh(n - 1, s, d, h); + cout << "Move disk " << n << " from rod " << s << " to rod " << d << endl; + toh(n - 1, h, s, d); +} + +int main() +{ + cout << "enter number of disks = " << endl; + int n; + cin >> n; + toh(n, 'A', 'B', 'C'); + return 0; +} diff --git a/Selection Sort b/Selection Sort index 74c560ebc..b6ef61871 100644 --- a/Selection Sort +++ b/Selection Sort @@ -1,48 +1,39 @@ -#include -using namespace std; - -void swap(int *xp, int *yp) -{ - int temp = *xp; - *xp = *yp; - *yp = temp; -} - -void selectionSort(int arr[], int n) -{ - int i, j, min_idx; - - // One by one move boundary of unsorted subarray - for (i = 0; i < n-1; i++) - { - // Find the minimum element in unsorted array - min_idx = i; - for (j = i+1; j < n; j++) - if (arr[j] < arr[min_idx]) - min_idx = j; - - // Swap the found minimum element with the first element - swap(&arr[min_idx], &arr[i]); - } -} - -/* Function to print an array */ -void printArray(int arr[], int size) -{ - int i; - for (i=0; i < size; i++) - cout << arr[i] << " "; - cout << endl; -} - -// Driver program to test above functions -int main() -{ - int arr[] = {64, 25, 12, 22, 11}; - int n = sizeof(arr)/sizeof(arr[0]); - selectionSort(arr, n); - cout << "Sorted array: \n"; - printArray(arr, n); - return 0; -} - +#include +using namespace std; +void swapping(int &a, int &b) { //swap the content of a and b + int temp; + temp = a; + a = b; + b = temp; +} +void display(int *array, int size) { + for(int i = 0; i> n; + int arr[n]; //create an array with given number of elements + cout << "Enter elements:" << endl; + for(int i = 0; i> arr[i]; + } + cout << "Array before Sorting: "; + display(arr, n); + selectionSort(arr, n); + cout << "Array after Sorting: "; + display(arr, n); +}