Skip to content
Merged
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
71 changes: 71 additions & 0 deletions C++/Cycle Detection in Directed Graph.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define mod 1000000007
#define mod9 998244353
#define raftaar \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL)
#define endl "\n"
bool dfs(int node, vector<int> adj[], vector<bool> &vis, vector<int> &check)
{
vis[node] = 1;
check[node] = 1;
for (auto x : adj[node])
{
if (!vis[x])
{
if (dfs(x, adj, vis, check))
{
return true;
}
}
if (check[x])
{
return true;
}
}
check[node] = 0;
return false;
}
bool isCyclic(int V, vector<int> adj[])
{
vector<bool> vis(V, 0);
vector<int> check(V, 0);

for (int i = 0; i < V; i++)
{
if (!vis[i])
{
if (dfs(i, adj, vis, check))
{
return true;
}
}
}

return false;
}
int main()
{
raftaar;
int node, edges;
cin >> node >> edges;
vector<int> adj[node];
for (int i = 0; i < edges; i++)
{
int u, v;
cin >> u >> v;
adj[u].push_back(v);
}
bool ans = isCyclic(node, adj);
if (ans)
{
cout << "The Graph contain cycle" << endl;
}
else
{
cout << "The Graph does not contain cycle" << endl;
}
}
79 changes: 79 additions & 0 deletions Java/Program 21/Quick_Sort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import java.util.Arrays;

public class QuickSortDemo{

public static void main(String args[]) {

// unsorted integer array
int[] unsorted = {6, 5, 3, 1, 8, 7, 2, 4};
System.out.println("Unsorted array :" + Arrays.toString(unsorted));

QuickSort algorithm = new QuickSort();

// sorting integer array using quicksort algorithm
algorithm.sort(unsorted);

// printing sorted array
System.out.println("Sorted array :" + Arrays.toString(unsorted));

}

}

class QuickSort {

private int input[];
private int length;

public void sort(int[] numbers) {

if (numbers == null || numbers.length == 0) {
return;
}
this.input = numbers;
length = numbers.length;
quickSort(0, length - 1);
}

/*
* This method implements in-place quicksort algorithm recursively.
*/
private void quickSort(int low, int high) {
int i = low;
int j = high;

// pivot is middle index
int pivot = input[low + (high - low) / 2];

// Divide into two arrays
while (i <= j) {
while (input[i] < pivot) {
i++;
}
while (input[j] > pivot) {
j--;
}
if (i <= j) {
swap(i, j);
// move index to next position on both sides
i++;
j--;
}
}

// calls quickSort() method recursively
if (low < j) {
quickSort(low, j);
}

if (i < high) {
quickSort(i, high);
}
}

private void swap(int i, int j) {
int temp = input[i];
input[i] = input[j];
input[j] = temp;
}
}
2 changes: 2 additions & 0 deletions Java/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
| [Program-18](https://github.com/swaaz/basicprograms/blob/814a1e60ae23d81158d8174666f23c9b7419e15e/Java/Program-18/Program%20Number%20to%20Word.java) | Program to calculate electricity bill
| [Program-19](https://github.com/swaaz/basicprograms/blob/814a1e60ae23d81158d8174666f23c9b7419e15e/Java/Program-19/Program%20Number%20to%20Word.java) | Program to calculate discount
| [Program-20](https://github.com/swaaz/basicprograms/blob/814a1e60ae23d81158d8174666f23c9b7419e15e/Java/Program-20/Program%20Number%20to%20Word.java) | Program to calculate batting average
| [Program-21]() | Quicksort Sorting Algorithm in java.


</div>

Expand Down