Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added two new problems in cpp section . #2294

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
82 changes: 82 additions & 0 deletions C++/Topological_sort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// C++ Program for topological sort using BFS

#include <bits/stdc++.h>
using namespace std;

bool topological_sort(int n, vector<int> adj[], vector<int> ans)
{

vector<int> indegree(n, 0);

for (int i = 0; i < adj.size(); i++)
indegree[adj[i][1]]++;

queue<int> 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<int> adj[n];
int u, v;

for (int i = 0; i < e; i++)
{
cin >> u >> v;

adj[u].push_back(v);
}

vector<int> 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;
}
22 changes: 22 additions & 0 deletions C++/Tower_Of_Hanoi.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include <iostream>
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;
}
87 changes: 39 additions & 48 deletions Selection Sort
Original file line number Diff line number Diff line change
@@ -1,48 +1,39 @@
#include <bits/stdc++.h>
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<iostream>
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<size; i++)
cout << array[i] << " ";
cout << endl;
}
void selectionSort(int *array, int size) {
int i, j, imin;
for(i = 0; i<size-1; i++) {
imin = i; //get index of minimum data
for(j = i+1; j<size; j++)
if(array[j] < array[imin])
imin = j;
//placing in correct position
swap(array[i], array[imin]);
}
}
int main() {
int n;
cout << "Enter the number of elements: ";
cin >> n;
int arr[n]; //create an array with given number of elements
cout << "Enter elements:" << endl;
for(int i = 0; i<n; i++) {
cin >> arr[i];
}
cout << "Array before Sorting: ";
display(arr, n);
selectionSort(arr, n);
cout << "Array after Sorting: ";
display(arr, n);
}