Skip to content

Commit e9b236b

Browse files
Merge branch 'master' into dependabot/maven/org.mockito-mockito-core-5.21.0
2 parents 9866126 + 4f40206 commit e9b236b

File tree

11 files changed

+763
-13
lines changed

11 files changed

+763
-13
lines changed

src/main/java/com/thealgorithms/sorts/BubbleSort.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@ class BubbleSort implements SortAlgorithm {
1010
/**
1111
* Implements generic bubble sort algorithm.
1212
*
13+
* Time Complexity:
14+
* - Best case: O(n) – array is already sorted.
15+
* - Average case: O(n^2)
16+
* - Worst case: O(n^2)
17+
*
18+
* Space Complexity: O(1) – in-place sorting.
19+
*
1320
* @param array the array to be sorted.
1421
* @param <T> the type of elements in the array.
1522
* @return the sorted array.

src/main/java/com/thealgorithms/sorts/HeapSort.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,20 @@
11
package com.thealgorithms.sorts;
22

33
/**
4-
* Heap Sort Algorithm Implementation
4+
* Heap Sort algorithm implementation.
5+
*
6+
* Heap sort converts the array into a max-heap and repeatedly extracts the maximum
7+
* element to sort the array in increasing order.
8+
*
9+
* Time Complexity:
10+
* - Best case: O(n log n)
11+
* - Average case: O(n log n)
12+
* - Worst case: O(n log n)
13+
*
14+
* Space Complexity: O(1) – in-place sorting
515
*
616
* @see <a href="https://en.wikipedia.org/wiki/Heapsort">Heap Sort Algorithm</a>
17+
* @see SortAlgorithm
718
*/
819
public class HeapSort implements SortAlgorithm {
920

src/main/java/com/thealgorithms/sorts/InsertionSort.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,23 @@
11
package com.thealgorithms.sorts;
22

3+
/**
4+
* Generic Insertion Sort algorithm.
5+
*
6+
* Standard insertion sort iterates through the array and inserts each element into its
7+
* correct position in the sorted portion of the array.
8+
*
9+
* Sentinel sort is a variation that first places the minimum element at index 0 to
10+
* avoid redundant comparisons in subsequent passes.
11+
*
12+
* Time Complexity:
13+
* - Best case: O(n) – array is already sorted (sentinel sort can improve slightly)
14+
* - Average case: O(n^2)
15+
* - Worst case: O(n^2) – array is reverse sorted
16+
*
17+
* Space Complexity: O(1) – in-place sorting
18+
*
19+
* @see SortAlgorithm
20+
*/
321
class InsertionSort implements SortAlgorithm {
422

523
/**

src/main/java/com/thealgorithms/sorts/MergeSort.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,16 @@ class MergeSort implements SortAlgorithm {
1313
private Comparable[] aux;
1414

1515
/**
16-
* Generic merge sort algorithm implements.
16+
* Generic merge sort algorithm.
1717
*
18-
* @param unsorted the array which should be sorted.
19-
* @param <T> Comparable class.
20-
* @return sorted array.
18+
* Time Complexity:
19+
* - Best case: O(n log n)
20+
* - Average case: O(n log n)
21+
* - Worst case: O(n log n)
22+
*
23+
* Space Complexity: O(n) – requires auxiliary array for merging.
24+
*
25+
* @see SortAlgorithm
2126
*/
2227
@Override
2328
public <T extends Comparable<T>> T[] sort(T[] unsorted) {

src/main/java/com/thealgorithms/sorts/QuickSort.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,16 @@
88
class QuickSort implements SortAlgorithm {
99

1010
/**
11-
* This method implements the Generic Quick Sort
11+
* Generic Quick Sort algorithm.
1212
*
13-
* @param array The array to be sorted Sorts the array in increasing order
13+
* Time Complexity:
14+
* - Best case: O(n log n) – pivot splits array roughly in half each time.
15+
* - Average case: O(n log n)
16+
* - Worst case: O(n^2) – occurs when pivot consistently produces unbalanced splits.
17+
*
18+
* Space Complexity: O(log n) – recursion stack, in-place sorting.
19+
*
20+
* @see SortAlgorithm
1421
*/
1522
@Override
1623
public <T extends Comparable<T>> T[] sort(T[] array) {

src/main/java/com/thealgorithms/sorts/SelectionSort.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,16 @@
22

33
public class SelectionSort implements SortAlgorithm {
44
/**
5-
* Sorts an array of comparable elements in increasing order using the selection sort algorithm.
5+
* Generic Selection Sort algorithm.
66
*
7-
* @param array the array to be sorted
8-
* @param <T> the class of array elements
9-
* @return the sorted array
7+
* Time Complexity:
8+
* - Best case: O(n^2)
9+
* - Average case: O(n^2)
10+
* - Worst case: O(n^2)
11+
*
12+
* Space Complexity: O(1) – in-place sorting.
13+
*
14+
* @see SortAlgorithm
1015
*/
1116
@Override
1217
public <T extends Comparable<T>> T[] sort(T[] array) {

src/main/java/com/thealgorithms/sorts/TopologicalSort.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,16 @@
1111
* a linked list. A Directed Graph is proven to be acyclic when a DFS or Depth First Search is
1212
* performed, yielding no back-edges.
1313
*
14-
* https://en.wikipedia.org/wiki/Topological_sorting
14+
* Time Complexity: O(V + E)
15+
* - V: number of vertices
16+
* - E: number of edges
1517
*
16-
* @author Jonathan Taylor (https://github.com/Jtmonument)
18+
* Space Complexity: O(V + E)
19+
* - adjacency list and recursion stack in DFS
20+
*
21+
* Reference: https://en.wikipedia.org/wiki/Topological_sorting
22+
*
23+
* Author: Jonathan Taylor (https://github.com/Jtmonument)
1724
* Based on Introduction to Algorithms 3rd Edition
1825
*/
1926
public final class TopologicalSort {
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
package com.thealgorithms.datastructures.graphs;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertNotNull;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
/**
9+
* Unit tests for the BellmanFord algorithm implementation.
10+
* Tests cover various graph scenarios including:
11+
* - Simple weighted graphs
12+
* - Graphs with negative weights
13+
* - Single vertex graphs
14+
* - Disconnected graphs
15+
* - Linear path graphs
16+
*/
17+
class BellmanFordTest {
18+
19+
@Test
20+
void testSimpleGraph() {
21+
// Create a simple graph with 5 vertices and 8 edges
22+
// Graph visualization:
23+
// 1
24+
// /|\
25+
// 6 | 7
26+
// / | \
27+
// 0 5 2
28+
// \ | /
29+
// 8 | -2
30+
// \|/
31+
// 4---3
32+
// 9
33+
BellmanFord bellmanFord = new BellmanFord(5, 8);
34+
bellmanFord.addEdge(0, 1, 6);
35+
bellmanFord.addEdge(0, 4, 8);
36+
bellmanFord.addEdge(1, 2, 7);
37+
bellmanFord.addEdge(1, 4, 5);
38+
bellmanFord.addEdge(2, 3, -2);
39+
bellmanFord.addEdge(2, 4, -3);
40+
bellmanFord.addEdge(3, 4, 9);
41+
bellmanFord.addEdge(4, 3, 7);
42+
43+
// Verify edge array creation
44+
assertNotNull(bellmanFord.getEdgeArray());
45+
assertEquals(8, bellmanFord.getEdgeArray().length);
46+
}
47+
48+
@Test
49+
void testGraphWithNegativeWeights() {
50+
// Graph with negative edge weights (but no negative cycle)
51+
BellmanFord bellmanFord = new BellmanFord(4, 5);
52+
bellmanFord.addEdge(0, 1, 4);
53+
bellmanFord.addEdge(0, 2, 5);
54+
bellmanFord.addEdge(1, 2, -3);
55+
bellmanFord.addEdge(2, 3, 4);
56+
bellmanFord.addEdge(1, 3, 6);
57+
58+
assertNotNull(bellmanFord.getEdgeArray());
59+
assertEquals(5, bellmanFord.getEdgeArray().length);
60+
}
61+
62+
@Test
63+
void testSingleVertexGraph() {
64+
// Graph with single vertex and no edges
65+
BellmanFord bellmanFord = new BellmanFord(1, 0);
66+
assertNotNull(bellmanFord.getEdgeArray());
67+
assertEquals(0, bellmanFord.getEdgeArray().length);
68+
}
69+
70+
@Test
71+
void testLinearGraph() {
72+
// Linear graph: 0 -> 1 -> 2 -> 3
73+
BellmanFord bellmanFord = new BellmanFord(4, 3);
74+
bellmanFord.addEdge(0, 1, 2);
75+
bellmanFord.addEdge(1, 2, 3);
76+
bellmanFord.addEdge(2, 3, 4);
77+
78+
assertNotNull(bellmanFord.getEdgeArray());
79+
assertEquals(3, bellmanFord.getEdgeArray().length);
80+
}
81+
82+
@Test
83+
void testEdgeAddition() {
84+
BellmanFord bellmanFord = new BellmanFord(3, 3);
85+
86+
bellmanFord.addEdge(0, 1, 5);
87+
bellmanFord.addEdge(1, 2, 3);
88+
bellmanFord.addEdge(0, 2, 10);
89+
90+
// Verify all edges were added
91+
assertNotNull(bellmanFord.getEdgeArray());
92+
assertEquals(3, bellmanFord.getEdgeArray().length);
93+
}
94+
95+
@Test
96+
void testGraphWithZeroWeightEdges() {
97+
// Graph with zero weight edges
98+
BellmanFord bellmanFord = new BellmanFord(3, 3);
99+
bellmanFord.addEdge(0, 1, 0);
100+
bellmanFord.addEdge(1, 2, 0);
101+
bellmanFord.addEdge(0, 2, 1);
102+
103+
assertNotNull(bellmanFord.getEdgeArray());
104+
assertEquals(3, bellmanFord.getEdgeArray().length);
105+
}
106+
107+
@Test
108+
void testLargerGraph() {
109+
// Larger graph with 6 vertices
110+
BellmanFord bellmanFord = new BellmanFord(6, 9);
111+
bellmanFord.addEdge(0, 1, 5);
112+
bellmanFord.addEdge(0, 2, 3);
113+
bellmanFord.addEdge(1, 3, 6);
114+
bellmanFord.addEdge(1, 2, 2);
115+
bellmanFord.addEdge(2, 4, 4);
116+
bellmanFord.addEdge(2, 5, 2);
117+
bellmanFord.addEdge(2, 3, 7);
118+
bellmanFord.addEdge(3, 4, -1);
119+
bellmanFord.addEdge(4, 5, -2);
120+
121+
assertNotNull(bellmanFord.getEdgeArray());
122+
assertEquals(9, bellmanFord.getEdgeArray().length);
123+
}
124+
125+
@Test
126+
void testVertexAndEdgeCount() {
127+
BellmanFord bellmanFord = new BellmanFord(10, 15);
128+
assertEquals(10, bellmanFord.vertex);
129+
assertEquals(15, bellmanFord.edge);
130+
}
131+
132+
@Test
133+
void testMultipleEdgesBetweenSameVertices() {
134+
// Graph allowing multiple edges between same vertices
135+
BellmanFord bellmanFord = new BellmanFord(2, 3);
136+
bellmanFord.addEdge(0, 1, 5);
137+
bellmanFord.addEdge(0, 1, 3);
138+
bellmanFord.addEdge(1, 0, 2);
139+
140+
assertNotNull(bellmanFord.getEdgeArray());
141+
assertEquals(3, bellmanFord.getEdgeArray().length);
142+
}
143+
144+
@Test
145+
void testCompleteGraph() {
146+
// Complete graph with 4 vertices (6 edges for undirected equivalent)
147+
BellmanFord bellmanFord = new BellmanFord(4, 6);
148+
bellmanFord.addEdge(0, 1, 1);
149+
bellmanFord.addEdge(0, 2, 2);
150+
bellmanFord.addEdge(0, 3, 3);
151+
bellmanFord.addEdge(1, 2, 4);
152+
bellmanFord.addEdge(1, 3, 5);
153+
bellmanFord.addEdge(2, 3, 6);
154+
155+
assertNotNull(bellmanFord.getEdgeArray());
156+
assertEquals(6, bellmanFord.getEdgeArray().length);
157+
}
158+
}

0 commit comments

Comments
 (0)