primes = new ArrayList<>();
+ for (int i = 2; i <= n; i++) {
+ if (isPrime[i]) {
+ primes.add(i);
}
}
- return primes;
- }
-
- /**
- * @brief finds all of the prime numbers up to the given upper (inclusive) limit
- * @param n upper (inclusive) limit
- * @exception IllegalArgumentException n is non-positive
- * @return the array of all primes up to the given number (inclusive)
- */
- public static int[] findPrimesTill(int n) {
- return extractPrimes(sievePrimesTill(n));
- }
- private enum Type {
- PRIME,
- NOT_PRIME,
+ return primes.stream().mapToInt(Integer::intValue).toArray();
}
}
diff --git a/src/test/java/com/thealgorithms/maths/SieveOfEratosthenesTest.java b/src/test/java/com/thealgorithms/maths/SieveOfEratosthenesTest.java
index ebbd5df712fc..8d332f04c512 100644
--- a/src/test/java/com/thealgorithms/maths/SieveOfEratosthenesTest.java
+++ b/src/test/java/com/thealgorithms/maths/SieveOfEratosthenesTest.java
@@ -1,46 +1,23 @@
package com.thealgorithms.maths;
-import static org.junit.jupiter.api.Assertions.assertArrayEquals;
-import static org.junit.jupiter.api.Assertions.assertThrows;
-
+import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
-class SieveOfEratosthenesTest {
- @Test
- public void testfFindPrimesTill1() {
- assertArrayEquals(new int[] {}, SieveOfEratosthenes.findPrimesTill(1));
- }
-
- @Test
- public void testfFindPrimesTill2() {
- assertArrayEquals(new int[] {2}, SieveOfEratosthenes.findPrimesTill(2));
- }
-
- @Test
- public void testfFindPrimesTill4() {
- var primesTill4 = new int[] {2, 3};
- assertArrayEquals(primesTill4, SieveOfEratosthenes.findPrimesTill(3));
- assertArrayEquals(primesTill4, SieveOfEratosthenes.findPrimesTill(4));
- }
-
- @Test
- public void testfFindPrimesTill40() {
- var primesTill40 = new int[] {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37};
- assertArrayEquals(primesTill40, SieveOfEratosthenes.findPrimesTill(37));
- assertArrayEquals(primesTill40, SieveOfEratosthenes.findPrimesTill(38));
- assertArrayEquals(primesTill40, SieveOfEratosthenes.findPrimesTill(39));
- assertArrayEquals(primesTill40, SieveOfEratosthenes.findPrimesTill(40));
- }
+/**
+ * Unit tests for {@link SieveOfEratosthenes}.
+ */
+public final class SieveOfEratosthenesTest {
@Test
- public void testfFindPrimesTill240() {
- var primesTill240 = new int[] {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239};
- assertArrayEquals(primesTill240, SieveOfEratosthenes.findPrimesTill(239));
- assertArrayEquals(primesTill240, SieveOfEratosthenes.findPrimesTill(240));
+ void testPrimesUpTo30() {
+ int[] expected = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29};
+ Assertions.assertArrayEquals(expected, SieveOfEratosthenes.sieve(30));
}
@Test
- public void testFindPrimesTillThrowsExceptionForNonPositiveInput() {
- assertThrows(IllegalArgumentException.class, () -> SieveOfEratosthenes.findPrimesTill(0));
+ void testLessThanTwo() {
+ Assertions.assertArrayEquals(new int[0], SieveOfEratosthenes.sieve(1));
+ Assertions.assertArrayEquals(new int[0], SieveOfEratosthenes.sieve(0));
+ Assertions.assertArrayEquals(new int[0], SieveOfEratosthenes.sieve(-5));
}
}
From fe08e48a633e4ffeba68c75b0bd34ba14b9eb03b Mon Sep 17 00:00:00 2001
From: Mansi5164 <156164354+Mansi5164@users.noreply.github.com>
Date: Wed, 29 Oct 2025 18:49:00 +0530
Subject: [PATCH 7/8] Added Topological Sort using DFS
---
.../graph/TopologicalSortDFS.java | 54 +++++++++++++++++++
.../graph/TopologicalSortDFSTest.java | 44 +++++++++++++++
2 files changed, 98 insertions(+)
create mode 100644 src/main/java/com/thealgorithms/graph/TopologicalSortDFS.java
create mode 100644 src/test/java/com/thealgorithms/graph/TopologicalSortDFSTest.java
diff --git a/src/main/java/com/thealgorithms/graph/TopologicalSortDFS.java b/src/main/java/com/thealgorithms/graph/TopologicalSortDFS.java
new file mode 100644
index 000000000000..965134c14b5b
--- /dev/null
+++ b/src/main/java/com/thealgorithms/graph/TopologicalSortDFS.java
@@ -0,0 +1,54 @@
+package com.thealgorithms.graph;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Stack;
+
+/**
+ * Implementation of Topological Sort using Depth-First Search (DFS).
+ *
+ * Topological sorting for Directed Acyclic Graph (DAG) is a linear ordering
+ * of vertices such that for every directed edge u → v, vertex u comes before v
+ * in the ordering.
+ */
+public final class TopologicalSortDFS {
+
+ // Private constructor to prevent instantiation
+ private TopologicalSortDFS() {
+ throw new AssertionError("Cannot instantiate utility class");
+ }
+
+ /**
+ * Performs topological sorting on a directed acyclic graph.
+ *
+ * @param vertices the number of vertices in the graph
+ * @param adjacencyList the adjacency list representing the graph
+ * @return a list containing vertices in topologically sorted order
+ */
+ public static List topologicalSort(int vertices, List> adjacencyList) {
+ boolean[] visited = new boolean[vertices];
+ Stack stack = new Stack<>();
+
+ for (int i = 0; i < vertices; i++) {
+ if (!visited[i]) {
+ dfs(i, visited, stack, adjacencyList);
+ }
+ }
+
+ List result = new ArrayList<>();
+ while (!stack.isEmpty()) {
+ result.add(stack.pop());
+ }
+ return result;
+ }
+
+ private static void dfs(int node, boolean[] visited, Stack stack, List> adjacencyList) {
+ visited[node] = true;
+ for (int neighbor : adjacencyList.get(node)) {
+ if (!visited[neighbor]) {
+ dfs(neighbor, visited, stack, adjacencyList);
+ }
+ }
+ stack.push(node);
+ }
+}
diff --git a/src/test/java/com/thealgorithms/graph/TopologicalSortDFSTest.java b/src/test/java/com/thealgorithms/graph/TopologicalSortDFSTest.java
new file mode 100644
index 000000000000..9c0c523971be
--- /dev/null
+++ b/src/test/java/com/thealgorithms/graph/TopologicalSortDFSTest.java
@@ -0,0 +1,44 @@
+package com.thealgorithms.graph;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+/**
+ * Unit tests for {@link TopologicalSortDFS}.
+ */
+public final class TopologicalSortDFSTest {
+
+ @Test
+ void testSimpleGraph() {
+ int vertices = 6;
+ List> adjacencyList = new ArrayList<>();
+ for (int i = 0; i < vertices; i++) {
+ adjacencyList.add(new ArrayList<>());
+ }
+
+ adjacencyList.get(5).add(2);
+ adjacencyList.get(5).add(0);
+ adjacencyList.get(4).add(0);
+ adjacencyList.get(4).add(1);
+ adjacencyList.get(2).add(3);
+ adjacencyList.get(3).add(1);
+
+ List result = TopologicalSortDFS.topologicalSort(vertices, adjacencyList);
+
+ // A valid topological order is one of the possible ones
+ List expected = Arrays.asList(5, 4, 2, 3, 1, 0);
+ Assertions.assertTrue(result.containsAll(expected) && expected.containsAll(result));
+ }
+
+ @Test
+ void testEmptyGraph() {
+ int vertices = 0;
+ List> adjacencyList = new ArrayList<>();
+ List result = TopologicalSortDFS.topologicalSort(vertices, adjacencyList);
+ Assertions.assertTrue(result.isEmpty());
+ }
+}
From e987dc8db92d5d1858fea98e9485970c9f28abf8 Mon Sep 17 00:00:00 2001
From: Mansi5164 <156164354+Mansi5164@users.noreply.github.com>
Date: Wed, 29 Oct 2025 21:02:47 +0530
Subject: [PATCH 8/8] Add Topological Sort using DFS with unit tests
---
.../KadanesAlgorithmTest.java | 24 +++++++++++++++++++
1 file changed, 24 insertions(+)
create mode 100644 src/main/java/com/thealgorithms/dynamicprogramming/KadanesAlgorithmTest.java
diff --git a/src/main/java/com/thealgorithms/dynamicprogramming/KadanesAlgorithmTest.java b/src/main/java/com/thealgorithms/dynamicprogramming/KadanesAlgorithmTest.java
new file mode 100644
index 000000000000..063b31f3f44f
--- /dev/null
+++ b/src/main/java/com/thealgorithms/dynamicprogramming/KadanesAlgorithmTest.java
@@ -0,0 +1,24 @@
+package com.thealgorithms.dynamicprogramming;
+
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+public class KadanesAlgorithmTest {
+ @Test
+ public void testPositiveNumbers() {
+ int[] arr = {1, 2, 3, 4};
+ Assertions.assertEquals(10, KadanesAlgorithm.maxSubArraySum(arr));
+ }
+
+ @Test
+ public void testNegativeNumbers() {
+ int[] arr = {-1, -2, -3, -4};
+ Assertions.assertEquals(-1, KadanesAlgorithm.maxSubArraySum(arr));
+ }
+
+ @Test
+ public void testMixedNumbers() {
+ int[] arr = {-2, 1, -3, 4, -1, 2, 1, -5, 4};
+ Assertions.assertEquals(6, KadanesAlgorithm.maxSubArraySum(arr)); // [4, -1, 2, 1]
+ }
+}