From 7cf5d446dba53b19e9a46ceb77c93da9c3e96b49 Mon Sep 17 00:00:00 2001 From: Naman Manjkhola <77687018+NamanManjkhola@users.noreply.github.com> Date: Sun, 31 Oct 2021 21:40:48 +0530 Subject: [PATCH 1/3] Added Quick_Sort.java --- Java/Program 21/Quick_Sort.java | 79 +++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 Java/Program 21/Quick_Sort.java diff --git a/Java/Program 21/Quick_Sort.java b/Java/Program 21/Quick_Sort.java new file mode 100644 index 0000000..fa83d35 --- /dev/null +++ b/Java/Program 21/Quick_Sort.java @@ -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; + } +} From 80ed4077b085984f9f7dc8f4d51cb84beb268ff9 Mon Sep 17 00:00:00 2001 From: Naman Manjkhola <77687018+NamanManjkhola@users.noreply.github.com> Date: Sun, 31 Oct 2021 21:42:07 +0530 Subject: [PATCH 2/3] Updated Readme.md --- Java/Readme.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Java/Readme.md b/Java/Readme.md index a3a554a..ce2ecbe 100644 --- a/Java/Readme.md +++ b/Java/Readme.md @@ -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. + From 40a7f79ca930ec9e9704d928463512afcc7a8a08 Mon Sep 17 00:00:00 2001 From: Avnish Kumar <75949349+Avnish010@users.noreply.github.com> Date: Sat, 29 Oct 2022 16:15:30 +0530 Subject: [PATCH 3/3] Create Cycle Detection in Directed Graph.cpp --- C++/Cycle Detection in Directed Graph.cpp | 71 +++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 C++/Cycle Detection in Directed Graph.cpp diff --git a/C++/Cycle Detection in Directed Graph.cpp b/C++/Cycle Detection in Directed Graph.cpp new file mode 100644 index 0000000..6672f1a --- /dev/null +++ b/C++/Cycle Detection in Directed Graph.cpp @@ -0,0 +1,71 @@ +#include +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 adj[], vector &vis, vector &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 adj[]) +{ + vector vis(V, 0); + vector 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 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; + } +}